简体   繁体   中英

Android setting alarm to a past date

What happens should I add an alarm but set the starting date to a past date?

Does is get executed immediately or is it put in the queue and never executed?

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startDate, repeatingValue, alarmIntent);

文档中 ,如果startDate时间是过去的,则会立即触发警报。

If the date is in past then alarm will trigger immediately. However you may try to use setInexactRepeating instead of setRepeating :

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startDate, setInexactRepeating , alarmIntent);

From the setInexactRepeating() docs :

Schedule a repeating alarm that has inexact trigger time requirements; for example, an alarm that repeats every hour, but not necessarily at the top of every hour.

Actually AlarmManager works with the current time. So when you will set past date alarm then AlarmManager will execute

public void onReceive(Context context, Intent intent)
{
}

method.

我认为警报只设置了几个小时(至少通过Android用户界面),而不是特定的一天,这样它将在你设置的确切hour:minute开始。

As i can conclude from my previous experience with AlarmManager . Date that is in the past will trigger alarm immediately.

As far as I can tell, AlarmManager.set will execute now when the time is set to a past time, the documentation says as much. This sentence is missing for AlarmManager.setInexactRepeating , this alarm will not trigger when it's set for a past time, it will trigger at the next interval, starting from the given time.

AlarmManager.set and AlarmManager.setInexactRepeating are both calling setImpl, with the triggertime they got passed as parameter (checked in Android 7.1.2 sources). --> there's no difference in both methods, if the triggertime is in the past.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM