警报管理器:重复警报并不总是触发

编程入门 行业动态 更新时间:2024-10-23 02:05:31
本文介绍了警报管理器:重复警报并不总是触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试开发一种提醒系统,因此用户可以输入提醒天数(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); } }

稍后我在自定义 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 返回。

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 与相同的 Intent 分别为每个请求使用不同的请求代码。当传递给 AlarmManager 时,它们将按预期触发单个警报。

One way to differentiate PendingIntents 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 PendingIntents
  • or use similar mechanism.

然后您需要致电 AlarmManager.cancel(pi),分别使用每个 PendingIntent 取消关联的警报。

Then you need to call AlarmManager.cancel(pi) with each of these PendingIntents individually to cancel associated alarms.

更多推荐

警报管理器:重复警报并不总是触发

本文发布于:2023-11-27 03:52:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636469.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   管理器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!