AlarmManager doesn't start the alarm on time
I try to use AlarmManager to setup an alarm that happens after some seconds from now. Here is my code:
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(ALARM_ACTION);
PendingIntent alarmIntent = PendingIntent.getBroadca开发者_如何学编程st(getBaseContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar rightNow = Calendar.getInstance();
rightNow.add(Calendar.SECOND, NumOfSecond);
am.set(AlarmManager.RTC, rightNow.getTimeInMillis(), alarmIntent);
For example, if rightNow is 8:00AM and I hope my alarm happens after 14400 seconds, that is 12:00PM, so NumOfSecond will be set as 14400. But when the code runs what happens is the alarm not always happens exactly at 12:00PM, sometimes it will be delayed by 1 or 2 minutes, or even 5 minutes! Does any one know what the heck is happened here?
Try AlarmManager.RTC_WAKEUP
instead.
Because when you add the seconds to your calendar time, it will also change in time since you will compile and run this at a different time. Try to use the set method instead at the specific time you want to run your alarm.
精彩评论