0

I am working on an app that uses an alarm to do something periodically. The alarm is set when the phone has completed booting. The BroadcastReceiver receiving the alarm and starting a service gets disabled when battery goes low and enabled when battery is okay again:

@Override
public void onReceive(final Context context, final Intent intent) {
    if (intent != null && intent.getAction() != null) {
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
            // Set alarm
        } else if (action.equals(Intent.ACTION_BATTERY_LOW)) {
            setLocationAlarmReceiverEnabled(context, false);
        } else if (action.equals(Intent.ACTION_BATTERY_OKAY)) {
            setLocationAlarmReceiverEnabled(context, true);
        }
    }
}

private void setLocationAlarmReceiverEnabled(final Context context,
        final boolean enabled) {
    PackageManager packageManager = context.getPackageManager();
    ComponentName locationAlarmReceiver = new ComponentName(context,
            LocationAlarmReceiver.class);
    packageManager.setComponentEnabledSetting(locationAlarmReceiver,
        enabled ? PackageManager.COMPONENT_ENABLED_STATE_DEFAULT
                : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
        PackageManager.DONT_KILL_APP);
}

In a practical test I could confirm that the BroadcastReceiver"LocationAlarmReceiver" gets disabled when battery level goes down to 15% and, after the phone was connected to AC/USB, it gets enabled again when the battery level went up to 30%.

But now I have had the following scenario:

  1. Battery level goes down to 15%, "LocationAlarmReceiver" gets disabled
  2. Phone eventually turns itself off because battery is exhausted
  3. Phone is connected to AC/USB
  4. Phone is switched on and boots, continues to charge, battery level reaches 100%
  5. "LocationAlarmReceiver" is still disabled

I can only guess that the Intent.ACTION_BATTERY_OKAY intent was never delivered.

Is this observation correct? How can I make sure that the "LocationAlarmReceiver" gets enabled again?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Torsten Römer
  • 3,834
  • 4
  • 40
  • 53

1 Answers1

1

If you are not getting the call on Intent.ACTION_BATTERY_OKAY then you should enable the receiver when you get the call in ACTION_BOOT_COMPLETED check if your receiver is disabled then you should enable it.

Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
  • I thought about that so thanks for confirming, but I guess that could mean that the `BroadcastReceiver` gets enabled when battery is (still) low? – Torsten Römer Sep 05 '11 at 09:58
  • then you should check battery level before enabling the receiver and if battery level is above 15% then you can enable it.... – Vineet Shukla Sep 05 '11 at 10:00
  • Yes, and here is info on how to do that: [link](http://stackoverflow.com/questions/3661464/get-battery-level-before-broadcast-receiver-responds-for-intent-action-battery-ch) – Torsten Römer Sep 05 '11 at 10:09
  • Actually, this would not solve the problem: In the above scenario, if I would check the battery level on `ACTION_BOOT_COMPLETED`, it would still be low and the receiver would remain disabled. – Torsten Römer Sep 05 '11 at 12:10
  • for that you can implement a timer which will check for battery level after some time and if it is not ok then you can set the timer again.. – Vineet Shukla Sep 05 '11 at 13:17
  • ...and it gets way too complex for something very simple, and a timer that checks every x minutes if battery level is okay again probably uses more battery than I can ever save by disabling the receiver. I appreciate your help, but I don't feel like my question is answered. – Torsten Römer Sep 05 '11 at 13:37
  • if your battery is ok then you should disable the timer and when boot takes place then you can start timer again .. – Vineet Shukla Sep 05 '11 at 13:40
  • check this: http://stackoverflow.com/questions/4461147/use-alarm-manager-to-set-alarm-in-android, http://stackoverflow.com/questions/4461147/use-alarm-manager-to-set-alarm-in-android – Vineet Shukla Sep 05 '11 at 13:44