android Alarmmanager 不会长时间重复

发布于 2024-12-01 10:23:36 字数 635 浏览 0 评论 0原文

我确信我做错了什么,但当我设置警报执行以下操作时,我无法让警报管理器多次触发:

AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, ReminderAlarmReceiver.class);
i.setData(Uri.parse(REMINDER_ID + getReminderID() + getTriggerAtTime()));
i.putExtra(REMINDER_ID, reminderID);
i.putExtra(DiaryListItem.DIARY_ID, parentDiaryID);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
mgr.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, AlarmManager.INTERVAL_DAY;, pi);

它似乎在第一天触发,但在第二天不会触发。当我尝试每周或每月发出警报时,也会发生同样的情况。有没有更好的方法来设置长期警报?

I'm sure I'm doing something wrong but I can't get the alarm manager to trigger more than once when I setup an alarm doing the following:

AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, ReminderAlarmReceiver.class);
i.setData(Uri.parse(REMINDER_ID + getReminderID() + getTriggerAtTime()));
i.putExtra(REMINDER_ID, reminderID);
i.putExtra(DiaryListItem.DIARY_ID, parentDiaryID);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
mgr.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, AlarmManager.INTERVAL_DAY;, pi);

It seems to trigger the first day but doesn't trigger the next day. The same thing happens when I try to make a weekly or monthly alarm. Is there a better way of setting up long term alarms?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪化雨蝶 2024-12-08 10:23:36

您是否打算将分号放入 .setRepeating 中?我想如果它实际上仍然存在,你的运行将会崩溃:

mgr.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, AlarmManager.INTERVAL_DAY;, pi);

应该是

mgr.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, AlarmManager.INTERVAL_DAY, pi);

如果这只是一个拼写错误,你的 logcat 会给出任何类型的错误吗?

Did you mean to put that semicolon in your .setRepeating? I imagine your run would crash if it were actually still there:

mgr.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, AlarmManager.INTERVAL_DAY;, pi);

should be

mgr.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, AlarmManager.INTERVAL_DAY, pi);

If that was just a typo, does your logcat give any kind of error?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文