Eric
Eric

Reputation: 27

Very strange issue on setting the alarm

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

Answers (2)

Phantômaxx
Phantômaxx

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; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.

Upvotes: 2

Aitor Viana
Aitor Viana

Reputation: 933

In a nutshell

  • for API < 19 you can use set() method. For these APIs this will set an exact alarm
  • for 19 <= API < 23 you should use setExact() to set exact alarms, and set() to set inexact alarms
  • for API = 23, Doze mode was also introduced, so in addition if you also want your alarm to go off even when the phone is in Doze mode, you should use setExactAndAllowWhileIdle() method instead.

Upvotes: 0

Related Questions