当应用程序被强制关闭时,AlarmManager 不起作用 [英] AlarmManager does not work when app is force closed

查看:72
本文介绍了当应用程序被强制关闭时,AlarmManager 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AlarmManager 的文档开始

<块引用>

注意:警报管理器适用于您希望在特定时间运行应用程序代码的情况,即使您的应用程序当前未运行.对于正常的计时操作(滴答、超时等),使用 Handler 更容易也更高效.

但是,一旦我的应用程序关闭(从任务管理器强制关闭),我的警报就不起作用,并且永远不会在广播接收器内部调用 OnReceive 方法.我的目标是 4.x.

发生了什么?

解决方案

@Shrikant 所说的几乎就是答案.

更长的版本是,如果用户不得不手动强制关闭应用程序,Android 会假设应用程序出现问题.因此,在用户至少手动运行应用程序一次之前,不会启动与应用程序相关的所有活动(广播接收器、警报等).例如,在此状态下设备关闭和打开时,应用的启动 BroadcastReceiver 不会被调用,直到用户运行该应用,然后 next 设备启动事件将传递给应用的 BroadcastReceiver.>

此行为已由 Android 框架开发人员设计确认:https://groups.google.com/forum/?fromgroups=#!topic/android-developers/anUoem0qrxU

*为语法编辑添加了示例行为

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?

解决方案

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

这篇关于当应用程序被强制关闭时,AlarmManager 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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