Automatic update battery level in android
<p>I am using this code to show battery level in my app. It works fine but doesn't update battery level automatically.</p>

<p>What should I do to update the battery level automatically.</p>

<pre><code>   public class MainActivity extends Activity {


        private TextView txtBattery;

        private BroadcastReceiver mBatteryLevelReciver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {

                context.unregisterReceiver(this);
                //int rawLevel = intent.getIntExtra("level", -1);
                int rawLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                //int scale = intent.getIntExtra("scale", -1);
                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                int level = -1;
                if (rawLevel &gt;= 0 &amp;&amp; scale &gt; 0) {
                    level = (rawLevel * 100) / scale;
                }
                txtBattery.setText("Battery3 Level Remaining  :" + level + "%");
            }

        };

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            txtBattery = (TextView) findViewById(R.id.batterymeter_txt);
            batteryLevel();

        }

        private void batteryLevel() {
            IntentFilter batteryLevelFliter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
            registerReceiver(mBatteryLevelReciver, batteryLevelFliter);
        }
}
</code></pre>
