android停止应用不能接收广播

在Android开发中,如果应用被强制停止,可能会导致无法接收预先在manifest中注册的广播。尽管通常认为manifest中注册的广播在应用运行后都能接收到,但自Android 3.1起,系统对此有了新的控制机制。官方API说明指出,从3.1版开始,系统会跟踪处于停止状态的应用,并提供了控制它们从后台进程和其他应用启动的方法。默认情况下,广播Intent不会激活已停止的应用组件,但可以通过设置特定标志来改变这一行为。

对于android开发人员来说,可能会发生这样的情况当你强行终止一个应用程序时,该应用程序不能接受到广播(该广播时在manifest文件中进行注册的),这是为什么呢?在我的第一印象中:只要时在manifest文件中注册的广播,只要运行过一次之后无论应用是处于什么状态都应该能接收到广播的,然而在anroid3.1中对于这个问题做了详细的说明。

下面是引自Android官方API说明,地址 http://developer.android.com/about/versions/android-3.1.html#launchcontrols

原文如下

Launch controls on stopped applications

Starting from Android 3.1, the system's package manager keeps track of applications that are in a stopped state and provides a means of controlling their launch from background processes and other applications.

Note that an application's stopped state is not the same as an Activity's stopped state. The system manages those two stopped states separately.

The platform defines two new intent flags that let a sender specify whether the Intent should be allowed to activate components in stopped application.

When neither or both of these flags is defined in an intent, the default behavior is to include filters of stopped applications in the list of potential targets.

Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It does this to prevent broadcasts from background services from inadvertently or unnecessarily launching components of stoppped applications. A background service or application can override this behavior by adding theFLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents that should be allowed to activate stopped applications.

Applications are in a stopped state when they are first installed but are not yet launched and when they are manually stopped by the user (in Manage Applications).


原来在3.1之后为了系统的安全性,默认情况下当应用处于停止状态就不能收到广播,除非在发送广播的intent中添加一个标志FLAG_INCLUDE_STOPED_PACKAGES。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值