アラームを使用して定期的に何かを行うアプリに取り組んでいます。電話が起動を完了すると、アラームが設定されます。アラームのBroadcastReceiver
受信とサービスの開始は、バッテリーが少なくなると無効になり、バッテリーが再び正常になると有効になります。
@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);
}
実際のテストではBroadcastReceiver
、バッテリー レベルが 15% になると「LocationAlarmReceiver」が無効になり、電話が AC/USB に接続された後、バッテリー レベルが 30% になると再び有効になることが確認できました。
しかし、今、私は次のシナリオを持っています:
- バッテリー残量が 15% に低下し、「LocationAlarmReceiver」が無効になります
- バッテリーが消耗しているため、電話は最終的に自動的にオフになります
- 電話は AC/USB に接続されています
- 電話の電源を入れて起動し、充電を続け、バッテリー レベルが 100% に達する
- 「LocationAlarmReceiver」はまだ無効です
Intent.ACTION_BATTERY_OKAY
インテントが配信されなかったと推測することしかできません。
この観察は正しいですか?「LocationAlarmReceiver」が再び有効になるようにするにはどうすればよいですか?