Nicholas Muir Nicholas Muir - 1 year ago 89
Android Question

AlarmManager alarm not starting exactly at the change of minute

I am doing an alarm clock and although using exact alarms it is not starting at the change of minute:

For example I set is at 7:30 it will start somewhere between 7:30:00 - 7:31 (have not been able to check exact time).

I call the alarm like this:

// Create the alarm
CustomAlarmManager alarmManager = new CustomAlarmManager(getActivity());
alarmManager.scheduleRepeatingAlarm(getActivity(), alarmID, alarmHour, alarmMinute);

Which runs this function to create the alarm:

public void scheduleRepeatingAlarm(Context context, int alarmID, int alarmHour, int alarmMinute) {

// Create an intent to go to the notifications reciever class
Intent intent = new Intent(context, AlarmNotificationReciever.class);

// Bundle information and add to the intent for use later
Bundle extras = new Bundle();
extras.putInt("AlarmId", alarmID);
extras.putInt("AlarmHour", alarmHour);
extras.putInt("AlarmMinute", alarmMinute);

// Create a pending intent for the alarm id and intent
PendingIntent pIntent = PendingIntent.getBroadcast(context,
alarmID, intent, PendingIntent.FLAG_CANCEL_CURRENT);

// Create a calender variable with the time for the alarm
Calendar calender = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, alarmHour);
calender.set(Calendar.MINUTE, alarmMinute);

// Create a variable for the current alarm time
LocalTime currentAlarmTime = LocalTime.parse(alarmHour+":"+alarmMinute+":"+"00");

// Create a variable for the local time
LocalTime localDeviceTime =;

// If the alarm has already elapsed today add one day to it so it runs for tomorrow
if (localDeviceTime.isAfter(currentAlarmTime)) {

calender.add(Calendar.DATE, 1);
System.out.println("The time of day for the alarm has already past rescheduling for tomorrow");

// For the different versions of operating system set off a single shot alarm
if(Build.VERSION.SDK_INT >= 23) {
calender.getTimeInMillis(), pIntent);
} else if(Build.VERSION.SDK_INT >= 19) {
mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), pIntent);
} else {
mAlarmManager.set(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), pIntent);

I am assuming the problem is going to be with these lines here:

Calendar calender = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, hour);
calender.set(Calendar.MINUTE, minute);

And this line when setting the alarm:

mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), pIntent);

But I am not sure why that would be not starting exactly on the change of minute.

Thanks for your help

Answer Source

Calendar.getInstance() returns a Calendar instance set to the current time (and date).

You're setting the hour and minutes of this Calendar instance, but the seconds remain unmodified.

Change the following:

Calendar calender = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, hour);
calender.set(Calendar.MINUTE, minute);

To this:

Calendar calender = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, hour);
calender.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download