任务终止后的 AlarmManager

发布于 2024-11-16 11:03:42 字数 1226 浏览 4 评论 0原文

SilenceReceiver silenceReceiver = new SilenceReceiver();
IntentFilter silenceFilter = new IntentFilter(SilenceReceiver.SILENCE_ALARM);
registerReceiver(silenceReceiver, silenceFilter);

AwakeReceiver awakeReceiver = new AwakeReceiver();
IntentFilter awakeFilter = new IntentFilter(AwakeReceiver.WAKE_ALARM);
registerReceiver(awakeReceiver, awakeFilter);

GregorianCalendar sleepCalendar = new GregorianCalendar();
sleepCalendar.set(Calendar.SECOND, sleepCalendar.get(Calendar.SECOND)+5);
long triggerTime = sleepCalendar.getTimeInMillis();

GregorianCalendar awakeCalendar = new GregorianCalendar();
awakeCalendar.set(Calendar.SECOND, awakeCalendar.get(Calendar.SECOND)+30);
long awakeTime = awakeCalendar.getTimeInMillis();

AlarmManager alarm = (AlarmManager) this.getSystemService(Service.ALARM_SERVICE);
PendingIntent sleepIntent = PendingIntent.getBroadcast(this, 0, new   Intent(SilenceReceiver.SILENCE_ALARM), 0);
alarm.set(AlarmManager.RTC_WAKEUP, triggerTime, sleepIntent);
PendingIntent awakeIntent = PendingIntent.getBroadcast(this, 0, new Intent(AwakeReceiver.WAKE_ALARM), 0);
alarm.set(AlarmManager.RTC_WAKEUP, awakeTime, awakeIntent);

问题是,如果任务被终止,我的 AwakeReceiver 永远不会触发。我能做些什么吗?

SilenceReceiver silenceReceiver = new SilenceReceiver();
IntentFilter silenceFilter = new IntentFilter(SilenceReceiver.SILENCE_ALARM);
registerReceiver(silenceReceiver, silenceFilter);

AwakeReceiver awakeReceiver = new AwakeReceiver();
IntentFilter awakeFilter = new IntentFilter(AwakeReceiver.WAKE_ALARM);
registerReceiver(awakeReceiver, awakeFilter);

GregorianCalendar sleepCalendar = new GregorianCalendar();
sleepCalendar.set(Calendar.SECOND, sleepCalendar.get(Calendar.SECOND)+5);
long triggerTime = sleepCalendar.getTimeInMillis();

GregorianCalendar awakeCalendar = new GregorianCalendar();
awakeCalendar.set(Calendar.SECOND, awakeCalendar.get(Calendar.SECOND)+30);
long awakeTime = awakeCalendar.getTimeInMillis();

AlarmManager alarm = (AlarmManager) this.getSystemService(Service.ALARM_SERVICE);
PendingIntent sleepIntent = PendingIntent.getBroadcast(this, 0, new   Intent(SilenceReceiver.SILENCE_ALARM), 0);
alarm.set(AlarmManager.RTC_WAKEUP, triggerTime, sleepIntent);
PendingIntent awakeIntent = PendingIntent.getBroadcast(this, 0, new Intent(AwakeReceiver.WAKE_ALARM), 0);
alarm.set(AlarmManager.RTC_WAKEUP, awakeTime, awakeIntent);

The problem is if the task is killed my AwakeReceiver never gets trigger. Is there anything I can do about this?

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

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

发布评论

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

评论(1

卸妝后依然美 2024-11-23 11:03:42

在清单中而不是在代码中注册广播接收器的意图过滤器。

Register the intent filters for the broadcast receiver in the manifest instead of in code.

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