FLAG_INCLUDE_STOPPED_PACKAGES 问题

作者: 苍风忍 | 来源:发表于2017-03-21 10:37 被阅读0次

    发送重要intent时,为了确保能收到这个intent,需添加flag: FLAG_INCLUDE_STOPPED_PACKAGES。

    这个flag 的注释很容易误导人,注释说,设置了这个flag后,intent可以找到当前被stopped的应用。 当没有设置FLAG_EXCLUDE_STOPPED_PACKAGES 这个flag时,默认按照 设置了FLAG_INCLUDE_STOPPED_PACKAGES 的行为进行。 如果同时设置了FLAG_EXCLUDE_STOPPED_PACKAGES 和 FLAG_INCLUDE_STOPPED_PACKAGES 时,也按照设置了FLAG_INCLUDE_STOPPED_PACKAGES 的行为进行。

    If set, this intent will always match any components in packages that are currently stopped. This is the default behavior when FLAG_EXCLUDE_STOPPED_PACKAGES is not set. If both of these flags are set, this one wins (it allows overriding of exclude for places where the framework may automatically set the exclude flag).

    看到注释后理所当然的认为只要不人为去添加一个FLAG_EXCLUDE_STOPPED_PACKAGES,就应该是按照设置了FLAG_INCLUDE_STOPPED_PACKAGES 的行为进行。 但是跟踪源码发现系统居然默认加了个exclude的flag,从ActivityManagerService中broadcastIntentLocked的源码看实际是添加了FLAG_EXCLUDE_STOPPED_PACKAGES:

    `
    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);
    
        if (DEBUG_BROADCAST_LIGHT) Slog.v(
            TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent
            + " ordered=" + ordered + " userid=" + userId);
        if ((resultTo != null) && !ordered) {
            Slog.w(TAG, "Broadcast " + intent + " not ordered but result callback requested!");
        }
    
        ...
    
        }
    

    `

    且android3.1的update说明中也说明系统默认为所有intent加了exclude flag。 android 3.1 release note

    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).

    这样的话如果开发者没有手动为intent加上FLAG_INCLUDE_STOPPED_PACKAGES的话,都会受到影响

    相关文章

      网友评论

        本文标题:FLAG_INCLUDE_STOPPED_PACKAGES 问题

        本文链接:https://www.haomeiwen.com/subject/qcwwnttx.html