当时间设置为 0:00 时立即调用警报
Alarm being called instantaneously when time set to 0:00
我正在制作一个闹钟,该闹钟会像其他任何闹钟一样在用户选择的时间和分钟启动。当时间设置为 0:00 时,我发现了一个问题,闹钟会立即激活。
我有一个自定义的 AlarmManager
,我在其中调用 schedule 重复,如下所示:
CustomAlarmManager alarmManager = new CustomAlarmManager(getActivity());
alarmManager.scheduleRepeatingAlarm(getActivity(),
alarmID, alarmHour, alarmMinute);
对于有问题的示例,正在传递这些值:
Id = 619
Hour = 0
Minute = 0
这是setRepeatingAlarm()
函数:
public void scheduleRepeatingAlarm(Context context, int alarmID, int hour, int minute) {
System.out.println("schedule is running");
Intent intent = new Intent(context, AlarmNotificationReciever.class);
Bundle extras = new Bundle();
extras.putBoolean(KEY_REPEAT, true);
extras.putInt("AlarmId", alarmID);
intent.putExtras(extras);
PendingIntent pIntent = PendingIntent.getBroadcast(context,
alarmID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar calender = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, hour);
calender.set(Calendar.MINUTE, minute);
if (android.os.Build.VERSION.SDK_INT >= 19) {
setSingleExactAlarm(calender.getTimeInMillis(), pIntent);
} else {
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calender.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pIntent);
}
}
我假设问题出在这些行上:
Calendar calender = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, hour);
calender.set(Calendar.MINUTE, minute);
设置闹钟时的这一行:
setSingleExactAlarm(calender.getTimeInMillis(), pIntent);
对于其他时间和分钟的组合,它似乎有效并将其视为结束时间。但是对于小时 = 0 和分钟 = 0,它会立即触发。
在此先感谢您的帮助。
这是预期的行为。
Calendar.getInstance()
returns 一个 Calendar
实例设置为当前 date/time.
保持日期不变并将小时和分钟字段设置为 0
有效地设置了过去的闹钟(它被认为是错过的闹钟)。
来自setRepeating()
的documentation:
If the stated trigger time is in the past, the alarm will be triggered
immediately, with an alarm count depending on how far in the past the
trigger time is relative to the repeat interval.
您可以像这样执行检查:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
Calendar now = Calendar.getInstance();
if(calendar.before(now)) {
// handle this as you wish
// adding a day to calendar
calendar.add(Calendar.DATE, 1);
}
我正在制作一个闹钟,该闹钟会像其他任何闹钟一样在用户选择的时间和分钟启动。当时间设置为 0:00 时,我发现了一个问题,闹钟会立即激活。
我有一个自定义的 AlarmManager
,我在其中调用 schedule 重复,如下所示:
CustomAlarmManager alarmManager = new CustomAlarmManager(getActivity());
alarmManager.scheduleRepeatingAlarm(getActivity(),
alarmID, alarmHour, alarmMinute);
对于有问题的示例,正在传递这些值:
Id = 619
Hour = 0
Minute = 0
这是setRepeatingAlarm()
函数:
public void scheduleRepeatingAlarm(Context context, int alarmID, int hour, int minute) {
System.out.println("schedule is running");
Intent intent = new Intent(context, AlarmNotificationReciever.class);
Bundle extras = new Bundle();
extras.putBoolean(KEY_REPEAT, true);
extras.putInt("AlarmId", alarmID);
intent.putExtras(extras);
PendingIntent pIntent = PendingIntent.getBroadcast(context,
alarmID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar calender = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, hour);
calender.set(Calendar.MINUTE, minute);
if (android.os.Build.VERSION.SDK_INT >= 19) {
setSingleExactAlarm(calender.getTimeInMillis(), pIntent);
} else {
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calender.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pIntent);
}
}
我假设问题出在这些行上:
Calendar calender = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, hour);
calender.set(Calendar.MINUTE, minute);
设置闹钟时的这一行:
setSingleExactAlarm(calender.getTimeInMillis(), pIntent);
对于其他时间和分钟的组合,它似乎有效并将其视为结束时间。但是对于小时 = 0 和分钟 = 0,它会立即触发。
在此先感谢您的帮助。
这是预期的行为。
Calendar.getInstance()
returns 一个 Calendar
实例设置为当前 date/time.
保持日期不变并将小时和分钟字段设置为 0
有效地设置了过去的闹钟(它被认为是错过的闹钟)。
来自setRepeating()
的documentation:
If the stated trigger time is in the past, the alarm will be triggered immediately, with an alarm count depending on how far in the past the trigger time is relative to the repeat interval.
您可以像这样执行检查:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
Calendar now = Calendar.getInstance();
if(calendar.before(now)) {
// handle this as you wish
// adding a day to calendar
calendar.add(Calendar.DATE, 1);
}