AlarmManager 警报未在分钟变化时准确启动

AlarmManager alarm not starting exactly at the change of minute

我正在做一个闹钟,虽然使用精确的闹钟,但它不是在分钟变化时开始的:

例如我设置的是 7:30 它将在 7:30:00 - 7:31 之间的某处开始(无法检查确切时间)。

我是这样叫闹钟的:

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

运行这个函数来创建警报:

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

我假设问题出在这些行上:

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

设置闹钟时的这一行:

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

但我不确定为什么不能准确地在分钟变化时开始。

感谢您的帮助

Calendar.getInstance() returns 一个 Calendar 实例设置为 当前 时间(和日期)。

您正在设置此 Calendar 实例的 HOUR_OF_DAYMINUTE 字段,但 SECOND 字段仍具有其初始值,这就是这种行为。

更改以下内容:

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

为此:

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