0

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?

Pink Jazz
  • 784
  • 4
  • 13
  • 34
  • 1
    Both will require you to have a process running all of the time, which is already inefficient. This is why, as of Android O, what you want to write becomes substantially more intrusive to the user (a foreground service with corresponding `Notification`). – CommonsWare May 16 '17 at 18:39

1 Answers1

1

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)

IIIIIIIIIIIIIIIIIIIIII
  • 3,958
  • 5
  • 45
  • 70