alarm triggers even after finish time in android

感情迁移 提交于 2019-12-12 02:36:24

问题


I am creating small Alarm project which has week days option. (I am using alarm manager).

The alarm triggers perfectly on given time and day but the problem is that it also triggers any previous alarm if I change the day or time after to the given alarm time.

For eg.

If the alarm is set to ring at 5:00 AM Monday Wednesday Friday. - And the current time is 6:00PM Sunday.

And another alarm set for for 4:00PM Monday Wednesday Friday - The current time is 6:30PM Sunday.

For testing I changed the day to Tuesday 6:00PM - Immediately the two alarm triggers one by one for Monday's schedule.

How do I stop this specific alarm and trigger only next Monday rather immediately? Do I need to check the dates also???

Let me know!


回答1:


You can't really. The alarm manager bases everything off of the system time. If you change the system time manually it is going to act just as if all the time had passed between now and whenever you set it for.

Edit: You can find the source code for the stock alarm application. Here is a link to one of the relavent objects Alarms.

I didn't test it any to ensure but from what I can tell they are storing the time the event is supposed to fire, and if it suddenly finds that it is in the past it ignores the alarm instead of letting it trigger. Some other relavent classes to check out are SetAlarm and AlarmClock, search for them on grepcode if you want to follow the way they set and react to the alarms in the stock app.

But it is still important to point out that manually changing the system time can still be used to manipulate the alarms. If for instance you set an alarm for 10:00pm tomorrow, then manually jump the time to 9:59pm tomorrow. The alarm will fire after 1 minute.




回答2:


compare your alarm time with System.currentTimeMillis()

if your alarm time is smaller then the current time, than no need to set the alarm



来源:https://stackoverflow.com/questions/13505036/alarm-triggers-even-after-finish-time-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!