Android reschedule alarmmanager alarm when app is killed

为君一笑 提交于 2021-01-27 12:44:20

问题


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.

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.

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".

Unfortunately I can't find a decent solution to reschedule the alarm / local notifications if the app is terminated.

What is the best approach for this case?


回答1:


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.



来源:https://stackoverflow.com/questions/28140219/android-reschedule-alarmmanager-alarm-when-app-is-killed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!