简体   繁体   中英

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);
    intent.putExtras(extras);

    // 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 = LocalTime.now();

    // 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) {
        mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
                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

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

You're setting the HOUR_OF_DAY and MINUTE fields of this Calendar instance, but the SECOND field still have its initial value, that is the cause of this behaviour.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM