0

I want to monitor Android battery level, even when it is changed by one percentage. The following two conditions should be satisfied:

1- Because of efficiency I do not want to check the battery level by Polling or Periodically (not using alarm manager, Handler or anything else which leads to polling)

2- On the other hand, I need to receive changes in the battery level even if my application is not in the foreground or if it is killed.

Currently I am using this code:

IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
context.registerReceiver(new MyBatteryReceiver(), ifilter);

It works nice until the context (or my activity) is alive, i.e. MyBatteryReceiver receives changes in the battery level.

Others suggest to use null with the registerReceiver:

context.registerReceiver(null, ifilter);

This solution means that I should check the battery level periodically.

I have checked many posts including this one:

Android access battery stats or monitoring battery level through manifest

but none of them answered this question: How to monitor android battery level WITHOUT polling? Of course when the level is too low or too high we can use the following broadcasts:

<action android:name="android.intent.action.ACTION_BATTERY_LOW"/>
<action android:name="android.intent.action.ACTION_BATTERY_OKAY"/>

Unfortunately it is not possible to use a similar broadcast for changes in the battery level:

<action android:name="android.intent.action.BATTERY_CHANGED" />
Community
  • 1
  • 1
saeed khalafinejad
  • 1,139
  • 9
  • 22
  • 1
    This isn't possible. Your requirements (1 and 2) and orthogonal. There is no way to ask the OS to inform you of changes in the BATTERY level while your app is not running. The only way to do this is to poll. Also, the broadcast `Intent` is probably not sent every time the batter percentage changes by 1%. Each manufacturer can implement this any way they want, and there is no way for you to tell how often or with what delta the broadcast Intent will be sent. For help see also http://stackoverflow.com/questions/7624882/action-battery-changed-firing-like-crazy – David Wasser Oct 30 '13 at 16:42
  • Thanks, at leat now I am sure that the only solution is polling. Do you have any idea about the best interval to poll the battery level? Thanks in advance – saeed khalafinejad Oct 31 '13 at 05:42
  • No, sorry, I don't. I'm sure this depends on what your application is, what devices you are running it on, what the objective/goal of the polling is, etc. If you do this every 15 or 30 minutes I would think that wouldn't have much performance impact, but that's just a guess. – David Wasser Oct 31 '13 at 08:00
  • This post https://stackoverflow.com/questions/10189218/detect-changing-battery-state-at-every-percentage-in-android says that it is up to the Device-Manufacturer how often to update the battery values. On some Devices it seems you simply cannot get every 1% change but only 10% changes until battery is very low. – FrankKrumnow Nov 16 '17 at 14:48

0 Answers0