我正在创建一个闹钟应用程序。我可以设置PendingIntent
,取消它们,并在达到我使用BroadcastReceiver
设置的时间时使用我的AlarmManager
接收它们。然而,我发现了最近的问题。
以前,我已经能够在将来的任何时间设置闹钟,并且BroadcastReceiver不会接收" PendingIntent直到达到那个时间。我想我从未介绍过要设置的警报正好在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);
}
答案 0 :(得分:0)
我检查了警报设置的时间,并确认它们不是当前时间所以它们不应该关闭。在尝试设置多个警报并发现问题仍然存在之后,我从手机中卸载了应用程序,并对其进行了全新安装。事后一切都很好,并且仍然正常运作。