Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Battery in SDK

Is there a way to get battery information from the Android SDK? Such as battery life remaining and so on? I cannot find it through the docs.

like image 362
Jeremy Edwards Avatar asked Nov 26 '09 22:11

Jeremy Edwards


People also ask

How do you check battery on Android?

You can check your Android phone's battery status by navigating to Settings > Battery > Battery Usage.

What is battery manager in Android?

android.os.BatteryManager. The BatteryManager class contains strings and constants used for values in the Intent. ACTION_BATTERY_CHANGED Intent, and provides a method for querying battery and charging properties.

What is battery API?

The Battery Status API, more often referred to as the Battery API, provides information about the system's battery charge level and lets you be notified by events that are sent when the battery level or charging status change.


1 Answers

Here is a quick example that will get you the amount of battery used, the battery voltage, and its temperature.

Paste the following code into an activity:

@Override public void onCreate() {     BroadcastReceiver batteryReceiver = new BroadcastReceiver() {         int scale = -1;         int level = -1;         int voltage = -1;         int temp = -1;         @Override         public void onReceive(Context context, Intent intent) {             level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);             scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);             temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);             voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);             Log.e("BatteryManager", "level is "+level+"/"+scale+", temp is "+temp+", voltage is "+voltage);         }     };     IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);     registerReceiver(batteryReceiver, filter); } 

On my phone, this has the following output every 10 seconds:

ERROR/BatteryManager(795): level is 40/100 temp is 320, voltage is 3848

So this means that the battery is 40% full, has a temperature of 32.0 degrees celsius, and has voltage of 3.848 Volts.

like image 63
plowman Avatar answered Oct 22 '22 08:10

plowman