【源码】BroadcastReceiver广播发送流程

广播发送流程笔记ContextImpl //-----------------普通广播---------------------// public void sendBroadcast(Intent intent, String receiverPermission, int appOp) { warnIfCallingFromSystemProcess(); String resolvedType = intent.resolveTypeIfNeeded
摘要由CSDN通过智能技术生成

广播发送流程笔记

ContextImpl

1、sendBroadcast、sendOrderedBroadcast、sendStickyBroadcast

这里主要列举了三种发送的方法:普通广播、有序广播、粘性广播(过时)。
其中有序广播2是可以传入一个resultReceiver,可以确保这个receiver最后一定能收得到广播,这种发送方式是会创建一个InnerReceiver用来找到我们设置进去的resultReceiver


    //-----------------普通广播---------------------//

    public void sendBroadcast(Intent intent, String receiverPermission, int appOp) {
   

		...

        try {
   
            ActivityManager.getService().broadcastIntent(
                    mMainThread.getApplicationThread(), intent, resolvedType, null,
                    Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
                    getUserId());
        }
        ...
    }


    //-----------------有序广播  1---------------------//


    void sendOrderedBroadcast(Intent intent,
            String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
            Handler scheduler, int initialCode, String initialData,
            Bundle initialExtras, Bundle options) {
   
            
        IIntentReceiver rd = null;
        if (resultReceiver != null) {
   
            if (mPackageInfo != null) {
   
                if (scheduler == null) {
   
                    scheduler = mMainThread.getHandler();
                }
                rd = mPackageInfo.getReceiverDispatcher(
                    resultReceiver, getOuterContext(), scheduler,
                    mMainThread.getInstrumentation(), false);
            } else {
   
                if (scheduler == null) {
   
                    scheduler = mMainThread.getHandler();
                }
                rd = new LoadedApk.ReceiverDispatcher(
                        resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
            }
        }

		...

        try {
   
            ActivityManager.getService().broadcastIntent(
                mMainThread.getApplicationThread(), intent, resolvedType, rd,
                initialCode, initialData, initialExtras, receiverPermissions, appOp,
                    options, true, false, getUserId());
        }
        ...
    }



	//-----------------有序广播  2---------------------//

	@Override
    public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
   
        warnIfCallingFromSystemProcess();
        String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
        String[] receiverPermissions = receiverPermission == null ? null
                : new String[] {
   receiverPermission};
        try {
   
            intent.prepareToLeaveProcess(this);
            ActivityManager.getService().broadcastIntent(
                    mMainThread.getApplicationThread(), intent, resolvedType, null,
                    Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
                    null, true, false, getUserId());
        } catch (RemoteException e) {
   
            throw e.rethrowFromSystemServer();
        }
    }


    //------------------粘性广播(废弃)----------------------//


    @Override
    @Deprecated
    public void sendStickyBroadcast(Intent intent) {
   
    	...
        try {
   
            ActivityManager.getService().broadcastIntent(
                mMainThread.getApplicationThread(), intent, resolvedType, null,
                Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
                getUserId());
        }
    }

AMS

1、sendBroadcastIntent

    public final int broadcastIntent(IApplicationThread caller,
            Intent intent, String resolvedType, IIntentReceiver resultTo,
            int resultCode, String resultData, Bundle resultExtras,
            String[] requiredPermissions, int appOp, Bundle bOptions,
            boolean serialized, boolean sticky, int userId) {
   
        enforceNotIsolatedCaller("broadcastIntent");
        synchronized(this) {
   
            intent = verifyBroadcastLocked(intent);

            final ProcessRecord callerApp = getRecordForAppLocked(caller);
            final int callingPid = Binder.getCallingPid();
            final int callingUid = Binder.getCallingUid();

            final long origId = Binder.clearCallingIdentity();
            try {
   
                //调用方法broadcastIntentLocked
                return broadcastIntentLocked(callerApp,
                        callerApp != null ? callerApp.info.packageName : null,
                        intent, resolvedType, resultTo, resultCode, resultData, resultExtras,
                        requiredPermissions, appOp, bOptions, serialized, sticky,
                        callingPid, callingUid, callingUid, callingPid, userId);
            } finally {
   
                Binder.restoreCallingIdentity(origId);
            }
        }
    }

2、broadcastIntentLocked(重要)

这个方法主要做了几件事情:

  • 1、如果是粘性广播,将它添加到mStickyBroadcasts列表中
  • 2、找出对应的静态广播接收器动态广播接收器
  • 3、优先处理动态注册的并行
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值