I need to set a repeating alarm every X hours that would even fire in doze mode. However, the only Apis available in AlarmManager for Android 23 are setExactAndAllowWhileIdle and setAndAllowWhileIdle which are not for repeating alarms.
I am wondering if I should reschedule the alarm every time it fires? or is there any better solution?
Open your phone's Clock app . At the bottom, tap Alarm. On the alarm you want, tap the Down arrow . Cancel: To cancel an alarm scheduled to go off in the next 2 hours, tap Dismiss.
If you want to set multiple alarms (repeating or single), then you just need to create their PendingIntent s with different requestCode . If requestCode is the same, then the new alarm will overwrite the old one.
You can cancel the alarm like this: Intent intent = new Intent(this, Mote. class); PendingIntent pendingIntent = PendingIntent. getBroadcast(getApplicationContext(), 1253, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.
I am wondering if I should reschedule the alarm every time it fires?
That is exactly what you should do.
The idea behind doze is to attempt to prevent draining the battery. Repeated alarms drain battery, so the builtin way to repeat alarms by passing an extra parameter was removed in android 6. It can still be done, but as you were wondering, that requires you to manually reschedule the alarm.
Be sure to reschedule the alarm immediately when it fires, before doing anything else that could go wrong and prevent the alarm from being rescheduled.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With