微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

添加隐式广播异常?

如何解决添加隐式广播异常?

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

我尝试删除一些免税广播,希望找到它们的列表以了解如何免税,但是我没有运气。谁能指出我正确的方向?谢谢。

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。