searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

Android 未启动过的APP或service不能接收广播的问题

2024-04-08 03:11:12
95
0
安卓从Android3.1开始,新安装的程序就会被置于”stopped”状态,并且只有在至少手动启动一次后该程序才会改变状态,才能够正常接收到指定的广播消息。Android这样做的目的是防止广播无意或者不必要地开启未启动的APP后台服务,那如何来解决这个问题呢?
 
我们看看谷歌官方的说明:
 

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,也可以由另一个应用启动。

这里放一个解决方案样例:

1、在AndroidMainifest.xml注册广播的地方添加action,同时添加android:exported=”true”,如:
<receiver android:name=".receiver.LogUploadBroadcastReceiver" android:exported="true">
            <intent-filter>
                <action android:name="intent.action.ACTION_LOG_UPLOAD" />
 
                <action android:name="android.action.logsetted"/>
            </intent-filter>
        </receiver>
 
 
2、在发送广播的地方添加Intent.FLAG_INCLUDE_STOPPED_PACKAGES,如:
        Intent intent = new Intent();
        intent.setAction("intent.action.ACTION_LOG_UPLOAD");
        intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        sendBroadcast(intent);
 

 

0条评论
0 / 1000
朱****春
17文章数
3粉丝数
朱****春
17 文章 | 3 粉丝
原创

Android 未启动过的APP或service不能接收广播的问题

2024-04-08 03:11:12
95
0
安卓从Android3.1开始,新安装的程序就会被置于”stopped”状态,并且只有在至少手动启动一次后该程序才会改变状态,才能够正常接收到指定的广播消息。Android这样做的目的是防止广播无意或者不必要地开启未启动的APP后台服务,那如何来解决这个问题呢?
 
我们看看谷歌官方的说明:
 

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,也可以由另一个应用启动。

这里放一个解决方案样例:

1、在AndroidMainifest.xml注册广播的地方添加action,同时添加android:exported=”true”,如:
<receiver android:name=".receiver.LogUploadBroadcastReceiver" android:exported="true">
            <intent-filter>
                <action android:name="intent.action.ACTION_LOG_UPLOAD" />
 
                <action android:name="android.action.logsetted"/>
            </intent-filter>
        </receiver>
 
 
2、在发送广播的地方添加Intent.FLAG_INCLUDE_STOPPED_PACKAGES,如:
        Intent intent = new Intent();
        intent.setAction("intent.action.ACTION_LOG_UPLOAD");
        intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        sendBroadcast(intent);
 

 

文章来自个人专栏
Android系统开发
17 文章 | 1 订阅
0条评论
0 / 1000
请输入你的评论
0
0