添加隐式广播异常?

Adding an implicit broadcast exception?

我一直在深入研究 AOSP 代码,并试图找到 implicit broadcast exceptions 的定义位置。最后,我想了解如何允许某些广播并添加一个额外的广播。

我试过 grep-ing 一些被豁免的广播,希望找到它们的列表以了解它们是如何被豁免的,但我没有任何运气。谁能指出我正确的方向?谢谢

根据我在 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);