Reputation: 27
I have set three repeating daily alarms on 12:00,16:00 and 20:00. But I found that the first alarm would not activate on time, but instead go off 11:59:34, 12:00:12 and 12:00:56.
I don't know why this would happen, and I have read many documents, no one gives me an answer.
Please help me!
Here is my code:
//Set the Calendar
Calendar cal=Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 00);
cal.set(Calendar.SECOND, 00);
//set PendingIntent
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
alarmIntent.putExtra("alarmType", 3);
PendingIntent pendingIntentMoring = PendingIntent.getBroadcast(context, 51, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntentAfternoon = PendingIntent.getBroadcast(context, 52, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntentEvening = PendingIntent.getBroadcast(context, 53, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//clear the alarm manager before set the new alarm
managerMorning.cancel(pendingIntentMoring);
managerAfternoon.cancel(pendingIntentAfternoon);
managerEvening.cancel(pendingIntentEvening);
//set new alarm
managerMorning.setRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntentMoring);
managerAfternoon.setRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntentAfternoon);
managerEvening.setRepeating(AlarmManager.RTC_WAKEUP, cal3.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntentEvening);
//set Receiver
public void onReceive(Context context, Intent intent) {
Intent myMessageintent = new Intent(context, UpDateDiaryMessage.class);
myMessageintent.putExtra("alarmType", 3);
context.startService(myMessageintent);
}
I've tested it many times.
I can't get the alarm to activate 12:00:00, but I got two or three alarms around 12:00:00.
I don't know why it sometimes works and sometimes not.
Upvotes: 1
Views: 75
Reputation: 38098
As per the official docs:
Note: Beginning with API 19 (
KITKAT
) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; seesetWindow(int, long, long, PendingIntent)
andsetExact(int, long, PendingIntent)
. Applications whosetargetSdkVersion
is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.
Upvotes: 2
Reputation: 933
In a nutshell
set()
method. For these APIs this will set an exact alarmsetExact()
to set exact alarms, and set()
to set inexact alarmssetExactAndAllowWhileIdle()
method instead.Upvotes: 0