android - 添加隐式广播异常?

标签 android android-intent android-source android-broadcast

我一直在挖掘 AOSP 代码,并试图找到 implicit broadcast exceptions 的位置被定义。最终,我想了解如何允许某些广播并添加一个额外的广播。
我已经尝试对一些豁免广播进行 grep-ing,希望找到一份 list 以了解它们是如何被豁免的,但我没有任何运气。谁能指出我正确的方向?谢谢。

最佳答案

根据我在 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 被发送到的任何地方。这些标志在 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);

关于android - 添加隐式广播异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64147135/

相关文章:

android - 使用 Flash Builder 4.5 构建移动应用程序(IOS 和 Android)

android - 检索变量中的列数据时出现游标索引错误

android - 在 adobe air 中使用 android Intent

android - RelativeLayout 覆盖 RelativeLayout 组件中声明的组件

android - 使用 Cache Uri 作为 MediaStore.EXTRA_OUTPUT 时相机无法工作/保存

android - 尝试为摩托罗拉 Droid 构建 Android AOSP Gingerbread

android - Zygote 预加载 vs boot.art 加载

java - Android 无法重写 Thread 中的 Final 方法

java - 当我打开相机时应用程序崩溃

android - AOSP Build 无法在锤头上启动