Android battery level in percentage/Numbers Example


Its quite simple to get Android battery information programmatically in numbers/percentage. You need to know the basics of  broadcaster receiver first. Have a look at this here Before starting with the program, let me explain the stuffs used in this example.
This is a sticky broadcast containing the charging state, level, and other information about the battery.The android BatteryManager class contains strings and constants used for values in the ACTION_BATTERY_CHANGED Intent. Among the various constants available, we will be making use of two constants -BatteryManager.EXTRA_LEVEL and  BatteryManager.EXTRA_SCALE

Integer field containing the current battery level, from 0 to EXTRA_SCALE.

Integer containing the maximum battery level.

registerReceiver(BroadcastReceiver receiver, IntentFilter filter)
We need to register for Action_Battery_Changed broadcast in this method as described in the example. This will get invoked whenever this event occurs. Since this is a sticky Intent, it keeps on broadcasting once registered.
Android Battery Percentage

AndroidBatteryPercentage Example -  
package com.example.androidbatterypercentage;

import android.os.BatteryManager;
import android.os.Bundle;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
 private TextView batteryPercent;
  private void getBatteryPercentage() {
  BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
         public void onReceive(Context context, Intent intent) {
             int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
             int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
             int level = -1;
             if (currentLevel >= 0 && scale > 0) {
                 level = (currentLevel * 100) / scale;
             batteryPercent.setText("Battery Level Remaining: " + level + "%");
     IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
     registerReceiver(batteryLevelReceiver, batteryLevelFilter);
   /* @Override*/
    protected void onCreate(Bundle savedInstanceState) {
         batteryPercent = (TextView) this.findViewById(;

//    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(, menu);
        return true;
That's it, we are done with the example to get Android battery level in percentage using Android battery manager class.

Source code of this application


Write comments

Popular Posts

Find us on Facebook