AlarmManager does not work when app is force closed

爱⌒轻易说出口 提交于 2019-11-26 21:19:37

问题


Documentation for AlarmManager startes that

Note: The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running. For normal timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler.

However, once my application is closed (force close from task manager) my alarm does not work and the OnReceive method is never called inside the broadcast receiver. I am targeting 4.x.

What's happening?


回答1:


What @Shrikant says is pretty much the answer.

The longer verison is that it is assumed by Android that something is amiss with the app if the user had to force close it manually. Therefore all activities (BroadcastReceiver's, alarms, etc) related to the app will not be initiated until the app is run manually by the user at least once. For example, app's boot BroadcastReceiver will not be called when device is turned off and on in this state until the user runs the app, then the next device boot event will be delivered to the app's BroadcastReceiver.

This behavior is confirmed as by design by Android framework devs: https://groups.google.com/forum/?fromgroups=#!topic/android-developers/anUoem0qrxU

*edited for grammar & added an example behavior



来源:https://stackoverflow.com/questions/16401447/alarmmanager-does-not-work-when-app-is-force-closed

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