How much delay is there when setting an inexact al

2019-02-24 11:10发布

问题:

This question already has an answer here:

  • How inexact is setInexactRepeating() for AlarmManager? 1 answer

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.

回答1:

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.