4.0 及以后版本 Boot_COMPLETED广播不响应的问题

早上健哥QQ问我了。正好前几天我写了个游戏补丁也用到这块的东西,研究一下,整理如下

通过查询google 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.

FLAG_INCLUDE_STOPPED_PACKAGES — Include intent filters of stopped applications in the list of potential targets to resolve against.
FLAG_EXCLUDE_STOPPED_PACKAGES — Exclude intent filters of stopped applications from the list of potential targets.
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 the FLAG_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).

Notification of application first launch and upgrade

The platform adds improved notification of application first launch and upgrades through two new intent actions:

ACTION_PACKAGE_FIRST_LAUNCH — Sent to the installer package of an application when that application is first launched (that is, the first time it is moved out of a stopped state). The data contains the name of the package.
ACTION_MY_PACKAGE_REPLACED — Notifies an application that it was updated, with a new version was installed over an existing version. This is only sent to the application that was replaced. It does not contain any additional data. To receive it, declare an intent filter for this action. You can use the intent to trigger code that helps get your application back in proper running shape after an upgrade.
This intent is sent directly to the application, but only if the application was upgraded while it was in started state (not in a stopped state).

android3.1以后,packageManager增加了对“stop state”的应用的管理,这个stopped和Activity生命周期中的stop状态是完全两码事,指的是安装后从来没有启动过和被用户手动强制停止的应用,与此同时系统增加了2个Flag:FLAG_INCLUDE_STOPPED_PACKAGES和FLAG_EXCLUDE_STOPPED_PACKAGES ,来标识一个intent是否激活处于“stopped state”的应用。当2个Flag都不设置或者都进行设置的时候,采用的是FLAG_INCLUDE_STOPPED_PACKAGES的效果。


google给所有的广播intent默认加上FLAG_EXCLUDE_STOPPED_PACKAGES效果卓著,能在一定程度上增加安全系数,还能提高效率,但是RECEIVE_BOOT_COMPLETED广播如果用户没有运行过应用,就不会响应了。


    解决这个问题还有一种途径,google允许应用和后台服务通过给广播intent设置FLAG_INCLUDE_STOPPED_PACKAGES来唤醒处于“stopped state”的程序,也就是用户自己写的广播intent可以控制这个机制,但是系统自带的广播intent,由于不能修改,就只能接受这个现实了。




你可能感兴趣的:(4.0 及以后版本 Boot_COMPLETED广播不响应的问题)