添加隐式广播例外?

4

我一直在查看AOSP代码,试图找到隐式广播异常的定义位置。最终,我希望既能理解某些广播如何被允许,又能添加一个额外的广播。

我尝试使用grep命令搜索已免除的广播以找到它们的列表,以了解它们是如何被免除的,但我没有找到任何结果。有没有人能指点我正确的方向?谢谢。

1个回答

4

根据我在AOSP 9.0.0_r35的经验,AOSP源代码中没有隐式广播例外列表的定义。

默认情况下,广播不会发送到已停止的应用程序(请参见文件ActivityManagerService.java,函数broadcastIntentLocked)。

final int broadcastIntentLocked(ProcessRecord callerApp,
            String callerPackage, Intent intent, String resolvedType,
            IIntentReceiver resultTo, int resultCode, String resultData,
            Bundle resultExtras, String[] requiredPermissions, int appOp, Bundle bOptions,
            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);
    ...
}

对于隐式广播异常,AOSP 在每次发送广播意图时手动添加标志 Intent.FLAG_RECEIVER_INCLUDE_BACKGROUNDIntent.FLAG_INCLUDE_STOPPED_PACKAGES。这些标志在 Intent.java 中定义如下:
/**
 * If set, this intent will always match any components in packages that
 * are currently stopped.  This is the default behavior when
 * {@link #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).
 */
public static final int FLAG_INCLUDE_STOPPED_PACKAGES = 0x00000020;

  /**
 * If set, the broadcast will always go to manifest receivers in background (cached
 * or not running) apps, regardless of whether that would be done by default.  By
 * default they will only receive broadcasts if the broadcast has specified an
 * explicit component or package name.
 *
 * NOTE: dumpstate uses this flag numerically, so when its value is changed
 * the broadcast code there must also be changed to match.
 *
 * @hide
 */
public static final int FLAG_RECEIVER_INCLUDE_BACKGROUND = 0x01000000;

例如,ACTION_TIMEZONE_CHANGED 的示例在 AlarmManagerService.java 中发送。
Intent intent = new Intent(Intent.ACTION_TIMEZONE_CHANGED);
intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING
        | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND
        | Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS);
intent.putExtra("time-zone", zone.getID());
getContext().sendBroadcastAsUser(intent, UserHandle.ALL);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接