I'm using AlarmManager
to schedule some periodical updates for my widget app. This is how I do schedule:
context.getSystemService<AlarmManager>()!!
.setInexactRepeating(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + refreshInterval.intervalMillis,
refreshInterval.intervalMillis,
pendingIntent
)
My pending intent looks like this:
PendingIntent.getBroadcast(
context,
RQ_CODE_BASE + id,
getStartIntent(context, appWidgetId),
PendingIntent.FLAG_UPDATE_CURRENT
)
As you see I'm using non wake up, inexact repeating alarm. Everything seems to be working fine while phone is awake and all schedules are executed right after AlarmManager
triggers alarm.
Let intervalMillis
be equal to 60k (1 minute). Then when phone is in sleep for 10 minutes, after bringing it back to 'life', I see that instead of triggering my PendingIntent
only once, it is executing PendingIntent
action 10 times one by another like there were queued. I thought that using PendingIntent.FLAG_UPDATE_CURRENT
flag will cause previous pending alarm action to be cancelled and new one will be scheduled after phone is awake but unfortunately it is not working that way.
Is there any way to accomplish desired behaviour? I could not find any AlarmMananger
methods that will allow me to lookup tasks queue for the particular pending intent.
I will be grateful for any kind of help/suggestions.
This is one of the downsides of using repeating alarms. I wouldn't use a repeating alarm for this. If you want the alarm to go off every X minutes, then schedule a single alarm for X minutes from now. When that alarm triggers, do whatever you want and then schedule a single alarm that goes off X minutes from now.
Single alarms are more predictable, they don't "queue", and you can use exact alarms if necessary.