Android, easy way to get battery stats

Hello everybody,

Many times I was trying to get battery usage stats, especially charge level. I didn’t find any cool way to do that quickly and forgot about it.

Today, I’m accidentally find a simple and interesting way to get all possible battery statistic:

$~ adb shell dumpsys battery

You will get output:

Current Battery Service state:
AC powered: false
AC capacity: 500000
USB powered: true
status: 5
health: 2
present: true
level: 100
scale: 100
voltage:4201
temperature: 271 <———- Battery temperature! %)
technology: Li-poly <———- Battery technology! %)

Sure it is cool, when you are able to use adb 🙂 But how can I get it from my app? Simply!

~$ adb shell

Device shell:

# dumpsys battery

— same output

Code snippet to get battery stats pragmatically:

public static void dumpBatteryInfo() {

try {
String cmd = “dumpsys battery“;
Log.i(“###### ********“, “EXECUTING CMD: ” + cmd);
Process script = Runtime.getRuntime().exec(cmd);
BufferedReader in = new BufferedReader(
new InputStreamReader(script.getInputStream()));

Log.i(“##########“, “****** >> Battery stats *******“);

String line = null;
while ((line = in.readLine()) != null) {
Log.i(“#############“, line);
}

Log.i(“##########“, “******/Battery stats *******“);

} catch (Exception ex) {
ex.printStackTrace();
}
}

Oh, yep, just forget, you have to add into the android manifest file DUMP permission:

So, you are able to parse string information very simply using any approach you like!

I decided to continue investigation of possible dumpsys using, enjoy 🙂

Related topic, how to get battery stats via Intent

Happy dumping! (:

Best regards,
Yahor