应用程式被杀死时,Android重新安排AlarmManager警报 [英] Android reschedule alarmmanager alarm when app is killed

查看:93
本文介绍了应用程式被杀死时,Android重新安排AlarmManager警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个应用程序,可以安排多个本地通知来提醒用户做某事.当年的每个月都应该发出通知.

I've developed an app to schedule multiple local notifications to remind users to do something. Every month in the current year there should an notification be raised.

这些本地通知是使用"AlarmManager"安排的.在"BroadcastReceiver"的OnRetrieve中创建并引发通知.

These local notifications are scheduled using an "AlarmManager". A notification is created and raised in the OnRetrieve of a "BroadcastReceiver".

在应用终止(由用户)或重新启动设备之前,一切正常.

It works all fine until the app is terminated (by user) or the device is rebooted.

经过一些研究,我发现了重新启动设备后重新安排警报/本地通知的解决方案=>使用带有"ActionBootCompleted"作为意图过滤器的BroadcastReceiver,然后在"OnReceive"中重新安排通知.

After some research I found the solution to reschedule the alarm / local notifications if the device is rebooted => using a BroadcastReceiver with "ActionBootCompleted" as intent filter and then reschedule the notifications in the "OnReceive".

不幸的是,如果应用终止,我找不到合适的解决方案来重新安排警报/本地通知.

这种情况下最好的方法是什么?

What is the best approach for this case?

推荐答案

尝试将其作为后台服务运行.当用户首次打开应用程序时,请调用服务OnCreate().确保该服务为START_STICKY,因此除非您明确告知,否则无法停止该服务.然后将您的AlarmManagers放入服务中.

Try to run this as a background service. When the user opens up the app for the first time, call the service OnCreate(). Make sure the service is START_STICKY so it cannot be stopped unless you explicitly tell it to. Then place your AlarmManagers inside the service.

这篇关于应用程式被杀死时,Android重新安排AlarmManager警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆