Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Development: Show Battery Level

I've been searching for this and only find really messed up things. Isn't there a easy way to show the battery level like 21% on a toast or Textview? Or how can i achieve this?

//Simon

like image 665
carefacerz Avatar asked Nov 28 '22 09:11

carefacerz


1 Answers

To get the battery level right now, call:

registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

(note: typing that in from memory, please adjust if needed)

This will return an Intent with various extras documented on the BatteryManager class. Use BatteryManager.EXTRA_LEVEL and BatteryManager.EXTRA_SCALE to determine the percentage remaining.

If you need to know over a period of time as the battery changes, use the BroadcastReceiver approach @Augusto outlined in his answer.

like image 188
CommonsWare Avatar answered Dec 16 '22 00:12

CommonsWare