Search code examples
androidbroadcastreceiverbatterybatterylevelbatterymanager

Monitoring Battery Level programmatically in Android - BroadcastReceiver vs Handler


I was wondering, what would be a more efficient way to monitor the battery level programmatically in Android - using a BroadcastReciever that continuously monitors the battery level, or a Handler that checks the battery level every 10 minutes by using postDelayed calls using a Runnable?

I would like to warn the user when the battery level drops below a specific percent. Which way would be more efficient?


Solution

  • For checking the current battery level of the device you want to use an BraodcastReceiver which listens to Intent.ACTION_BATTERY_CHANGED events. This way your receiver only performs work in case the battery level really changed.

    In case of using an Handler which checks the battery level every 10 minutes there are guaranteed scenarios where the battery level has not changed or where the battery level changed that fast(while charging) that you miss some battery level changes. (which is an clear disadvantage compared to the BroadcastReceiver)