Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain Battery Stats in Android at runtime?

I want to display Battery level stats in my app. How can we obtain such information like Battery Power, Battery Voltage, etc.?

like image 651
Muhammad Maqsoodur Rehman Avatar asked Feb 15 '10 13:02

Muhammad Maqsoodur Rehman


People also ask

How do I check battery on time?

So, how to check screen time on Android? From your device's home screen, pull down the Quick Settings panel, and tap on the battery icon that you'll see in the upper right corner. Once you do that, a Battery menu will show up, displaying detailed info on what's draining your battery.

How do you check battery history on Android?

Go to Settings > Battery (the app's location may vary for your version of Android) and you'll see a history graph detailing which apps have been consuming the most battery power and how long you can use the device before the battery is completely drained off.

How do I check power consumption on Android?

Settings > Battery > Usage details Open Settings and tap on the Battery option. Next select Battery Usage and you'll be given a breakdown of all the apps that are draining your power, with the most-hungry ones at the top. Some phones will tell you how long each app has been actively used – others won't.


2 Answers

   BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            context.unregisterReceiver(this);
            int rawlevel = intent.getIntExtra("level", -1);
            int scale = intent.getIntExtra("scale", -1);
            int level = -1;
            if (rawlevel >= 0 && scale > 0) {
                level = (rawlevel * 100) / scale; /* This is your battery level */ 
            }
         }
    };
    IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}

or check out this guy (who wrote the code I posted): http://mihaifonoage.blogspot.com/2010/02/getting-battery-level-in-android-using.html

like image 122
Ulve Avatar answered Sep 18 '22 15:09

Ulve


Is this of any help?

public class Main extends Activity {
   private TextView contentTxt;
   private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
      @Override
      public void onReceive(Context arg0, Intent intent) {
         // TODO Auto-generated method stub
         int level = intent.getIntExtra("level", 0);
         contentTxt.setText(String.valueOf(level) + "%");
      }
   };

   @Override
   public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      setContentView(R.layout.main);
      contentTxt = (TextView) this.findViewById(R.id.monospaceTxt);
      this.registerReceiver(this.mBatInfoReceiver, 
         new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
   }
}

Also, see this.

like image 23
Anton Gogolev Avatar answered Sep 20 '22 15:09

Anton Gogolev