当闹钟时间与实时时间相差 1 小时时,将触发待处理意图 - Android



我正在创建一个闹钟应用程序。我可以设置PendingIntent,取消它们,并在达到我使用AlarmManager设置的时间时使用我的BroadcastReceiver接收它们。然而,我发现了一个最近的问题。

以前,我已经能够为将来的任何时间设置警报,并且在到达该时间之前,BroadcastReceiver 不会"接收"待定意图。我想我从来没有涵盖要设置的闹钟正好是 1 小时或更长时间(仅限整数(的情况。例如,当前时间是 11:54,我为 12:54 或 1:54、2:54 等设置了闹钟。当我这样做时,BroadcastReceiver 会收到 PendingIntent 并执行我告诉它执行的操作。

为什么会这样?当我将分钟更改为其他内容时,它不会发生,只有当分钟相同时,应用程序的行为就像我为当前时间设置闹钟一样。

这是我设置闹钟的方式:

public void scheduleAlarm(Context aContext) {
AlarmManager am = (AlarmManager) aContext.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(aContext, MyBroadcastReceiver.class);
String id = this.getId().replaceAll("[^0-9]+", "");      // this.getId returns a string such as "alarm1". We only need the "1".
PendingIntent alarmIntent = PendingIntent.getBroadcast(aContext, Integer.parseInt(id), intent, 0);
// "this" in this context is the Alarm object. So you can get the hour and minute from the timepicker used to set the alarm
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, this.getHour());
calendar.set(Calendar.MINUTE, this.getMinute());
long calendarTime = calendar.getTimeInMillis();
am.setExact(AlarmManager.RTC_WAKEUP, calendarTime, alarmIntent);
}

我检查了设置闹钟的时间,并确认它们不是当前时间,因此它们不应该响起。在尝试设置多个警报并看到问题仍在发生后,我从手机中卸载了该应用程序,并对其进行了全新安装。之后一切正常,仍然正常运行。

最新更新