AlarmManager doesn't start the alarm on time

Posted by user988635 on Stack Overflow See other posts from Stack Overflow or by user988635
Published on 2011-10-11T01:13:12Z Indexed on 2011/11/14 9:50 UTC
Read the original article Hit count: 291

Filed under:

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.getBroadcast(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?

© Stack Overflow or respective owner

Related posts about android