警报管理器:重复警报并不总是触发 [英] Alarm manager: repeating alarm not always fires
问题描述
我正在尝试开发一种提醒系统,因此用户可以输入提醒天数(MON,TUE ...)以及触发该提醒的时间(仅小时和分钟)。它可以是一周中的任何一天或一周中的几天。以下是用于设置这些提醒的代码:
I'm trying to develop a kind of reminders system, so user can enter days for reminders (MON, TUE...) and times on which this reminder will fire (only hour and minutes). It can be any day of the week or multiple days of the week. Here is the code for setting these reminders:
final AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
for (final Day day: reminder.getDays()) {
for (final ReminderTime reminderTime: reminder.getTimes()) {
final Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, reminderTime.getHour());
calendar.set(Calendar.MINUTE, reminderTime.getMinute());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.DAY_OF_WEEK, day.getCalendarDay());
if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
// final long daysDifference = DateUtils.getDaysDifference(calendar.getTimeInMillis(), System.currentTimeMillis());
// calendar.add(Calendar.DAY_OF_YEAR, (int) (daysDifference + 1));
calendar.add(Calendar.DAY_OF_YEAR, 7);
}
final Intent intent = createReminderIntent(context, reminder.getReminderType(), reminderTime, day);
final PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, sender);
}
}
稍后我在自定义<$ c $中收到的警报c> BroadcastReceiver 。
问题是,提醒不时发出,有时甚至不发出,在我看来,日子越来越糟。我想知道是错误还是我做错了?
The alarm I recieve later in custom BroadcastReceiver
.
The problem is that the reminder is firing from time to time and sometimes not, seems to me that something is woring with the days. I'm wondering were is the bug or what I'm doing wrong?
推荐答案
调用 PendingIntent时.get *(...)
具有相同的 Intent
,并请求代码具有相同的 PendingIntent $ c实例$ c>返回。
When calling PendingIntent.get*(...)
with the same Intent
and request code the same instance of PendingIntent
is returned.
AlarmManager
只能将一个规则与一个相关联PendingIntent
,这意味着只有您的代码中由 AlarmManager.setRepeating(...)
设置的最后一个警报实际上是活动的。
The AlarmManager
can have only one rule associated for one PendingIntent
which means that only the last alarm set by AlarmManager.setRepeating(...)
in your code is actually active. The others got overwritten by this last rule.
一种区分 PendingIntent
与相同的<$ c $的方法c> Intent 分别为每个请求使用不同的请求代码。当传递给 AlarmManager
时,它们将按预期触发单个警报。
One way to differentiate PendingIntent
s with the same Intent
is to use a different request code for each. When passed to AlarmManager
these will trigger individual alarms as expected.
遗憾的是,无法取消已定义的多个警报,例如按基本意图
,因此您必须
Sadly there's no way to cancel multiple alarms defined e.g. by base Intent
so you have to either
- 保留所有
PendingIntent
实例最初用于安排警报 - 保留所有请求代码并重建表示的
PendingIntent
s - 或使用类似的机制。
- keep all your
PendingIntent
instances used originally to schedule the alarms - keep all the request codes and reconstruct said
PendingIntent
s - or use similar mechanism.
然后您需要致电 AlarmManager.cancel(pi)
,分别使用每个 PendingIntent
取消关联的警报。
Then you need to call AlarmManager.cancel(pi)
with each of these PendingIntent
s individually to cancel associated alarms.
这篇关于警报管理器:重复警报并不总是触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!