2015年7月13日 星期一

Static broadcast receiver in the stopped application can't receive the broadcast

Android developer document:


Launch controls on stopped applications

Starting from Android 3.1, 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.

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.


Article form internet:



1. android 3.1中有一類package 叫做stopped package 它們就是那種安裝了但是從來沒有啟動過的apk,或者被使用者在程式管理裡面force stop了的apk

2. intent中新加了一組flagFLAG_INCLUDE_STOPPED_PACKAGESFLAG_EXCLUDE_STOPPED_PACKAGES),帶有FLAG_EXCLUDE_STOPPED_PACKAGES intentstopped package是不起作用的。

3. 系統對所有的廣播intent都加了flagFLAG_EXCLUDE_STOPPED_PACKAGES,當然boot complete廣播也不例外。

這裡需要注意一點: /system/app下面的apk都是非 stopped package。所以如果你的手機root了,那你可以把apk pushsystem/app


Conclusion:


This is why the SOS app can’t receive the HEADSET_PLUG broadcast when it is first installed but is not yet launched or manually stopped through Settings.

By marking the following command in ActivityManagerService experimentally, I have verified the SOS app can receive the HEADSET_PLUG broadcast successfully.

Source code:

frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

    private final int broadcastIntentLocked(ProcessRecord callerApp,

            String callerPackage, Intent intent, String resolvedType,

            IIntentReceiver resultTo, int resultCode, String resultData,

            Bundle map, String requiredPermission, int appOp,

            boolean ordered, boolean sticky, int callingPid, int callingUid,

            int userId) {

        intent = new Intent(intent);


        // By default broadcasts do not go to stopped apps.

        //intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES);

                        ……

沒有留言: