How much delay is there when setting an inexact alarm via AlarmManager? [duplicate]
Asked Answered
L

1

4

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.

Lucchesi answered 22/12, 2015 at 5:33 Comment(0)
I
5

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.

Internment answered 10/2, 2016 at 2:22 Comment(5)
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()?Thetos
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 25Internment
Thanks for your quick answer. you didn't mention in your answer that you are talking about setInexactRepeating() or setRepeating()Thetos
It depends on your targetSDK version. You can see this in the code here and here. 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.Internment
Telco Network providing time may slip thousands of years into the future, for whatever reason, so that is fine.Sniggle

© 2022 - 2024 — McMap. All rights reserved.