Launch controls on stoppedapplications
Starting from Android 3.1, the system's package manager keeps track ofapplications that are in a stopped state and provides a means
of controllingtheir launch from background processes and other applications.
Note that an application's stopped state is not the same as an Activity'sstopped state. The system manages those two stopped states
separately.
The platform defines two new intent flags that let a sender specifywhether the Intent should be allowed to activate components in
stoppedapplication.
FLAG_INCLUDE_STOPPED_PACKAGES —Include intent filters of stopped applications in the list of potential targetsto resolve against.
FLAG_EXCLUDE_STOPPED_PACKAGES —Exclude intent filters of stopped applications from the list of potentialtargets.
When neither or both of these flags is defined in an intent, the defaultbehavior is to include filters of stopped applications in
the list ofpotential targets.
Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGESto all broadcastintents. It does this to prevent broadcasts from background
services frominadvertently or unnecessarily launching components of stoppped applications.A background service or application can
override this behavior by adding theFLAG_INCLUDE_STOPPED_PACKAGES flag to broadcastintents that should be allowed to activate stopped
applications.
Applications are in a stopped state when they are first installed but are notyet launched and when they are manually stopped by the
user (in ManageApplications).
对于上述描述总结如下:
1、在Android3.1以后版本添加了标志FLAG_INCLUDE_STOPPED_PACKAGES和FLAG_EXCLUDE_STOPPED_PACKAGES,用于区分发送广播时是否启动激活那些未启动过或者被用户force stop的应用组件。当两个Flag都不设置或都设置的时候,默认操作是FLAG_INCLUDE_STOPPED_PACKAGES。
2、系统向所有的Intent的广播添加了FLAG_EXCLUDE_STOPPED_PACKAGES标志。它这样做是为了防止广播无意中的或不必要地开启组件的stoppped应用程序的后台服务。这样可以优化系统性能,提高安全性,不过对于监控类软件是一个沉重的打击。
3、用户给自定义的广播Intent添加FLAG_INCLUDE_STOPPED_PACKAGES,用于启动stop状态的应用组件。但是系统自带的广播intent,我们无能为力。
综上,在android3.1以后,系统加强了广播的限制,对于进程或者service长时间运行不退出没有有效的解决方案。总体来看,启动两个独立应用进程是一个不错的选择,一旦用户启动应用后,即使点击force stop,也可以由另一个应用启动。
这里放一个解决方案样例: