When using AlarmManager
to set an alarm, there could be a delay in which the alarm is triggered some time after the specified time unless you set an exact alarm. Is there any guarantee on what the range of this delay could be? I want to be a responsible developer and not use exact times if the delay is not more than, say, one minute. But I can't find any specifications on the delay in documentation. I would appreciate a resource that documents how the delay functions and its time specifications.
Asked
Active
Viewed 1,397 times
3

Jason Robinson
- 31,005
- 19
- 77
- 131
1 Answers
4
75% of either the recurrence interval [for a periodic alarm] or of the time from now to the desired delivery time, with a minimum delay/interval of 10 seconds, under which we will simply not defer the alarm.
From the Android source for AlarmManagerService for API19 (and still the same as of API23)
Requested Batch Window 1 Mins -> 1- 1¾ Mins 10 Mins -> 10-17½ Mins 30 Mins -> 30-52½ Mins 1 Hour -> 1- 1¾ Hours
It's also worth noting that while AlarmManagerService guards the window length to ensure that lengths greater than half a day are treated as suspicions (and rewritten to 1 hour), it doesn't provide similar sanity checks for trigger times.
As a result you can easily provide an RTC based value (System.currentTimeMillis()) with an elapsed based mode (ELAPSED_REALTIME) and end up with an alarm that's thousands of years in the future.

Kas Hunt
- 3,098
- 1
- 12
- 15
-
is this based on official document? what about other versions of android? can i be sure that in all android versions, my repeating alarm won't delayed more than 75% of its time interval? is it for setInexactRepeating() or setRepeating()? – Mehran Zamani Feb 08 '17 at 11:20
-
This is based on the Android source code (See the link in my answer) - which is the most authoritative documentation there is ;) As mentioned above, this function is unchanged from API19 to 23, and a quick check shows that the same function is unchanged through to [API 25](http://androidxref.com/7.1.1_r6/xref/frameworks/base/services/core/java/com/android/server/AlarmManagerService.java#708) – Kas Hunt Feb 08 '17 at 11:41
-
Thanks for your quick answer. you didn't mention in your answer that you are talking about setInexactRepeating() or setRepeating() – Mehran Zamani Feb 08 '17 at 11:45
-
It depends on your targetSDK version. You can see this in the code [here](http://androidxref.com/7.1.1_r6/xref/frameworks/base/core/java/android/app/AlarmManager.java#252) and [here](http://androidxref.com/7.1.1_r6/xref/frameworks/base/core/java/android/app/AlarmManager.java#335). If your targetSDK version is API19 (KitKat) or above, setRepeating() and setInexactRepeating() do the same thing (All alarms are inexact). If you target less than API19, setRepeating will be exact. – Kas Hunt Feb 08 '17 at 13:07
-
Telco Network providing time may slip thousands of years into the future, for whatever reason, so that is fine. – straya Aug 10 '22 at 02:11