Android进阶 四大组件的工作过程(三):广播的注册,发送和接收过程

news2024/11/27 4:18:26

Android进阶 四大组件的工作过程(三):广播的注册,发送和接收过程

在这里插入图片描述

导语

本篇文章是介绍四大组件工作过程的第三篇文章,在前两篇文章里我们已经介绍了Activity和Service的工作流程。而本篇文章,我们将介绍广播相关的工作流程。与Activity和Service相比,广播的使用频率在一般情况下远远低于前两者,但是作为四大组件之一,我们仍然需要了解其工作原理。我们就将其工作过程分为注册,发送和接收三个个过程来分别介绍。

前两篇文章:

  • Android进阶 四大组件的工作过程(一):Activity的工作过程
  • Android进阶 四大组件的工作过程(二):Service的工作过程

广播的注册

广播又可以分为静态广播的注册和动态广播的注册,静态广播是通过PackageManagerService解析XML清单文件来安装的,也就是在安装APK的时候进行的,这涉及到PackageManagerService,由于对这一块我还不是特别熟悉,这里就主要来介绍动态广播的注册了。

动态广播的注册

我们注册动态广播时,需要调用registerReceiver方法,这个方法是在ContextWrapper中实现的,其实具体来说和Service的启动十分相似,我们来看代码:

    public Intent registerReceiver(@Nullable BroadcastReceiver receiver, IntentFilter filter,
            int flags) {
        return mBase.registerReceiver(receiver, filter, flags);
    }

这里会调用到mBase的方法,这个mBase的实现类实际上看过Service的过程的话会十分熟悉,就是ContextImpl类,我们接着跳转到ContextImpl的方法中,最终会调用到ContextImpl的registerInternal方法:

 private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
            IntentFilter filter, String broadcastPermission,
            Handler scheduler, Context context, int flags) {
        IIntentReceiver rd = null;
        if (receiver != null) {
            if (mPackageInfo != null && context != null) {
                if (scheduler == null) {
                    scheduler = mMainThread.getHandler();
                }
                rd = mPackageInfo.getReceiverDispatcher(
                    receiver, context, scheduler,
                    mMainThread.getInstrumentation(), true); //1------1
            } else {
                if (scheduler == null) {
                    scheduler = mMainThread.getHandler();
                }
                rd = new LoadedApk.ReceiverDispatcher(
                        receiver, context, scheduler, null, true).getIIntentReceiver();//2------2
            }
        }
        try {
            ActivityThread thread = ActivityThread.currentActivityThread();
            Instrumentation instrumentation = thread.getInstrumentation();
            if (instrumentation.isInstrumenting()
                    && ((flags & Context.RECEIVER_NOT_EXPORTED) == 0)) {
                flags = flags | Context.RECEIVER_EXPORTED;
            }
            final Intent intent = ActivityManager.getService().registerReceiverWithFeature(
                    mMainThread.getApplicationThread(), mBasePackageName, getAttributionTag(),
                    AppOpsManager.toReceiverId(receiver), rd, filter, broadcastPermission, userId, //3------3
                    flags);
            if (intent != null) {
                intent.setExtrasClassLoader(getClassLoader());
                // TODO: determine at registration time if caller is
                // protecting themselves with signature permission
                intent.prepareToEnterProcess(ActivityThread.isProtectedBroadcast(intent),
                        getAttributionSource());
            }
            return intent;
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

这里我们分析一下这段代码,先看注释一处,如果mPackageInfo != null && context != null成立的话,就会获取拥有的IIntentReceiver对象,否则就会创建一个IIntentReceiver对象。从这个命名也可以看出来这个是用于跨进程通信,具体来说,这是用于广播的跨进程通信的。接着让我们直接跳到注释三处,这里非常熟悉,又会跳到AMS的registerReceiverWithFeature方法之中,并且会将之前获取到的IIntentReceiver对象给传入进去。所以接下来看AMS中的方法。

public Intent registerReceiverWithFeature(IApplicationThread caller, String callerPackage,
            String callerFeatureId, String receiverId, IIntentReceiver receiver,
            IntentFilter filter, String permission, int userId, int flags) {
		..........
        ArrayList<Intent> stickyIntents = null;
        ProcessRecord callerApp = null;
        final boolean visibleToInstantApps
                = (flags & Context.RECEIVER_VISIBLE_TO_INSTANT_APPS) != 0;

        int callingUid;
        int callingPid;
        boolean instantApp;
        synchronized(this) {
            if (caller != null) {
                callerApp = getRecordForAppLOSP(caller);//1--------1
                ..........
            Iterator<String> actions = filter.actionsIterator();//2---------2
			...........
            int[] userIds = { UserHandle.USER_ALL, UserHandle.getUserId(callingUid) };
            while (actions.hasNext()) {
                String action = actions.next();
                for (int id : userIds) {
                    ArrayMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(id);
                    if (stickies != null) {
                        ArrayList<Intent> intents = stickies.get(action);//3--------3
                        if (intents != null) {
                            if (stickyIntents == null) {
                                stickyIntents = new ArrayList<Intent>();
                            }
                            stickyIntents.addAll(intents);//4---------4
                        }
                    }
                }
                ...........
            }

           ............
        }


        ArrayList<Intent> allSticky = null;
        if (stickyIntents != null) {
            final ContentResolver resolver = mContext.getContentResolver();
            // Look for any matching sticky broadcasts...
            for (int i = 0, N = stickyIntents.size(); i < N; i++) {
                Intent intent = stickyIntents.get(i);
                // Don't provided intents that aren't available to instant apps.
                if (instantApp &&
                        (intent.getFlags() & Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS) == 0) {
                    continue;
                }
                if (filter.match(resolver, intent, true, TAG) >= 0) {
                    if (allSticky == null) {
                        allSticky = new ArrayList<Intent>();
                    }
                    allSticky.add(intent);//5---------5
                }
            }
        }

		............

        synchronized (this) {
            IApplicationThread thread;
            if (callerApp != null && ((thread = callerApp.getThread()) == null
                    || thread.asBinder() != caller.asBinder())) {
                // Original caller already died
                return null;
            }
            ReceiverList rl = mRegisteredReceivers.get(receiver.asBinder());//6----------6
            if (rl == null) {
                rl = new ReceiverList(this, callerApp, callingPid, callingUid,
                        userId, receiver);
                if (rl.app != null) {
                    final int totalReceiversForApp = rl.app.mReceivers.numberOfReceivers();
                    if (totalReceiversForApp >= MAX_RECEIVERS_ALLOWED_PER_APP) {
                        throw new IllegalStateException("Too many receivers, total of "
                                + totalReceiversForApp + ", registered for pid: "
                                + rl.pid + ", callerPackage: " + callerPackage);
                    }
                    rl.app.mReceivers.addReceiver(rl);
                } else {
                    try {
                        receiver.asBinder().linkToDeath(rl, 0);
                    } catch (RemoteException e) {
                        return sticky;
                    }
                    rl.linkedToDeath = true;
                }
                mRegisteredReceivers.put(receiver.asBinder(), rl);
            } ...........
            }
            BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage, callerFeatureId,
                    receiverId, permission, callingUid, userId, instantApp, visibleToInstantApps,
                    exported);
            if (rl.containsFilter(filter)) {
                Slog.w(TAG, "Receiver with filter " + filter
                        + " already registered for pid " + rl.pid
                        + ", callerPackage is " + callerPackage);
            } else {
                rl.add(bf);//7----------7
                if (!bf.debugCheck()) {
                    Slog.w(TAG, "==> For Dynamic broadcast");
                }
                mReceiverResolver.addFilter(getPackageManagerInternal().snapshot(), bf);//8--------8
            }
			...........
			return sticky;
        }
    }

这个方法的注释一处获得了调用方的进程接着在注释二处获得了过滤器的迭代器,很显然是之后用于遍历的。在注释三所在的While循环中,就使用这个迭代器遍历了过滤器中的每一个action标签并且获取到了对应的粘性Intent并在注释四处将其添加到AMS的粘性Intent列表中。注释五处则是将符合过滤器要求的粘性Intent添加到了粘性广播的Intent列表之中。

接着来到第二部分,注释六处根据传入的Binder获取到了ReceiverList接收器列表。这个ReceiverList是一个继承了
ArrayList< BroadcastFilter >的类,主要是来存储广播接收者和BroadcastFilter的。之后又会创建一个BroadcastFilter并且会在注释注释七处将其添加入ReceiverList中。最后在注释八处将BroadcastFilter也添加进自身(AMS)的mReceiverResolver,这样当AMS接收到广播时就可以从mReceiverResolver中找到对应的广播接收者。这个变量时IntentResolver类型的,通过它就可以识别Intent意图并执行响应的回调,具体我们在下一节介绍这个类。

IntentResolver类

IntentResolver是Android框架中的一个类,用于解析和匹配意图(Intent)。它在Android系统中起着关键的作用,用于处理各种组件之间的通信和交互。

IntentResolver的主要功能是根据给定的意图(Intent)进行组件匹配。它维护了一组注册的组件(如活动、服务、广播接收器),并根据意图的属性(如动作、类别、数据等)来匹配合适的组件。

具体来说,IntentResolver的主要任务包括:

  • 注册组件:当应用程序声明了活动、服务或广播接收器时,它们会通过注册机制将自己注册到IntentResolver中,以便能够被其他应用程序或系统发现和调用。

  • 匹配意图:当一个意图(Intent)被发送或启动时,IntentResolver会根据意图的属性对注册的组件进行匹配。它会遍历已注册的组件列表,逐个检查每个组件的过滤器(IntentFilter),并判断是否与意图匹配。

  • 返回匹配结果:IntentResolver会返回匹配的组件列表,供调用方选择合适的组件进行交互。根据不同的场景,可能会有多个组件与意图匹配,因此调用方可以根据自己的需求选择最合适的组件进行启动、发送广播等操作。

IntentResolver在Android系统的各个组件之间起着桥梁的作用,它使得不同应用程序之间能够进行组件间的通信和交互。它是实现Android组件之间高效通信的重要组成部分。

广播的发送过程

发送广播也可以有多种形式,这里我们以发送普通广播为例来介绍广播的发送过程。发送普通广播调用的是sendBroadcast方法,它同样是在ContextWrapper中实现的,同样是会由ContextImpl最终调用:

    public void sendBroadcast(Intent intent) {
        warnIfCallingFromSystemProcess();
        String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
        try {
            intent.prepareToLeaveProcess(this);
            ActivityManager.getService().broadcastIntentWithFeature(
                    mMainThread.getApplicationThread(), getAttributionTag(), intent, resolvedType,
                    null, Activity.RESULT_OK, null, null, null, null /*excludedPermissions=*/,
                    null, AppOpsManager.OP_NONE, null, false, false, getUserId());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

显然又是会跳转到AMS中,来看AMS中的方法:

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

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

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

这里注释一处的verifyBroadcastLocked方法将会验证广播的合法性,通过合法性验证后将会继续跳转到注释二的方法处:

final int broadcastIntentLocked(ProcessRecord callerApp, String callerPackage,
            @Nullable String callerFeatureId, Intent intent, String resolvedType,
            IIntentReceiver resultTo, int resultCode, String resultData,
            Bundle resultExtras, String[] requiredPermissions,
            String[] excludedPermissions, String[] excludedPackages, int appOp, Bundle bOptions,
            boolean ordered, boolean sticky, int callingPid, int callingUid,
            int realCallingUid, int realCallingPid, int userId,
            boolean allowBackgroundActivityStarts,
            @Nullable IBinder backgroundActivityStartsToken,
            @Nullable int[] broadcastAllowList) {
        intent = new Intent(intent);

        final boolean callerInstantApp = isInstantApp(callerApp, callerPackage, callingUid);
       ...............
        if ((receivers != null && receivers.size() > 0)
                || resultTo != null) {
            BroadcastQueue queue = broadcastQueueForIntent(intent); //1---------1
            BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, callerPackage,
                    callerFeatureId, callingPid, callingUid, callerInstantApp, resolvedType,
                    requiredPermissions, excludedPermissions, excludedPackages, appOp, brOptions,
                    receivers, resultTo, resultCode, resultData, resultExtras,
                    ordered, sticky, false, userId, allowBackgroundActivityStarts,
                    backgroundActivityStartsToken, timeoutExempt);

            if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Enqueueing ordered broadcast " + r);

            final BroadcastRecord oldRecord =
                    replacePending ? queue.replaceOrderedBroadcastLocked(r) : null;
            if (oldRecord != null) {
                // Replaced, fire the result-to receiver.
                if (oldRecord.resultTo != null) {
                    final BroadcastQueue oldQueue = broadcastQueueForIntent(oldRecord.intent);
                    try {
                        oldQueue.performReceiveLocked(oldRecord.callerApp, oldRecord.resultTo,
                                oldRecord.intent,
                                Activity.RESULT_CANCELED, null, null,
                                false, false, oldRecord.userId, oldRecord.callingUid, callingUid);
                    } catch (RemoteException e) {
                        Slog.w(TAG, "Failure ["
                                + queue.mQueueName + "] sending broadcast result of "
                                + intent, e);

                    }
                }
            } else {
                queue.enqueueOrderedBroadcastLocked(r);
                queue.scheduleBroadcastsLocked();//2--------2
            }
        } else {
            if (intent.getComponent() == null && intent.getPackage() == null
                    && (intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) {
                // This was an implicit broadcast... let's record it for posterity.
                addBroadcastStatLocked(intent.getAction(), callerPackage, 0, 0, 0);
            }
        }

        return ActivityManager.BROADCAST_SUCCESS;
    }

这个方法主要就是在注释一处获得了广播列表然后在注释二处调用scheduleBroadcastLocked方法,这个方法会向BroadcastQueue自身的Handler发送BROADCAST_INTENT_MSG的信息,然后触发其processNextBroadcast方法,最后会调用其processNextBroadcastLocked方法,这个方法就是为了将广播发送给广播接收者的。

广播的发送和接收过程

其实接下来就会涉及到广播的接收了,我们接上文,看processNextBroadcastLocked方法:

 final void processNextBroadcastLocked(boolean fromMsg, boolean skipOomAdj) {
        BroadcastRecord r;

       ..........

        if (fromMsg) {
            mBroadcastsScheduled = false;
        }

        // First, deliver any non-serialized broadcasts right away.
        while (mParallelBroadcasts.size() > 0) {
            r = mParallelBroadcasts.remove(0);
            r.dispatchTime = SystemClock.uptimeMillis();
            r.dispatchRealTime = SystemClock.elapsedRealtime();
            r.dispatchClockTime = System.currentTimeMillis();

     		............
            final int N = r.receivers.size();
			...........
            for (int i=0; i<N; i++) {
                Object target = r.receivers.get(i);
                if (DEBUG_BROADCAST)  Slog.v(TAG_BROADCAST,
                        "Delivering non-ordered on [" + mQueueName + "] to registered "
                        + target + ": " + r);
                deliverToRegisteredReceiverLocked(r,
                        (BroadcastFilter) target, false, i);  //1---------1
            }
            addBroadcastToHistoryLocked(r);
            if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Done with parallel broadcast ["
                    + mQueueName + "] " + r);
        }

        ............
    }

这里我们直接看注释一处的代码,这里会遍历广播的接收者列表,然后调用注释一处的deliverToRegisteredReceiverLocked方法将广播依次发送给接收者。

private void deliverToRegisteredReceiverLocked(BroadcastRecord r,
            BroadcastFilter filter, boolean ordered, int index) {
        boolean skip = false;
       ............
        try {
            if (DEBUG_BROADCAST_LIGHT) Slog.i(TAG_BROADCAST,
                    "Delivering to " + filter + " : " + r);
            if (filter.receiverList.app != null && filter.receiverList.app.isInFullBackup()) {
                // Skip delivery if full backup in progress
                // If it's an ordered broadcast, we need to continue to the next receiver.
                if (ordered) {
                    skipReceiverLocked(r);
                }
            } else {
                r.receiverTime = SystemClock.uptimeMillis();
                maybeAddAllowBackgroundActivityStartsToken(filter.receiverList.app, r);
                maybeScheduleTempAllowlistLocked(filter.owningUid, r, r.options);
                maybeReportBroadcastDispatchedEventLocked(r, filter.owningUid);
                performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver,//1--------1
                        new Intent(r.intent), r.resultCode, r.resultData,
                        r.resultExtras, r.ordered, r.initialSticky, r.userId,
                        filter.receiverList.uid, r.callingUid);
                // parallel broadcasts are fire-and-forget, not bookended by a call to
                // finishReceiverLocked(), so we manage their activity-start token here
                if (filter.receiverList.app != null
                        && r.allowBackgroundActivityStarts && !r.ordered) {
                    postActivityStartTokenRemoval(filter.receiverList.app, r);
                }
            }
            if (ordered) {
                r.state = BroadcastRecord.CALL_DONE_RECEIVE;
            }.........
        } 
    }

如果通过了前面的权限检查,这里就会继续调用注释一处的performReceiveLocked方法:

    void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver,
            Intent intent, int resultCode, String data, Bundle extras,
            boolean ordered, boolean sticky, int sendingUser,
            int receiverUid, int callingUid) throws RemoteException {
        // Send the intent to the receiver asynchronously using one-way binder calls.
        if (app != null) {
            final IApplicationThread thread = app.getThread();
            if (thread != null) {
                // If we have an app thread, do the call through that so it is
                // correctly ordered with other one-way calls.
                try {
                    thread.scheduleRegisteredReceiver(receiver, intent, resultCode, //1------1
                            data, extras, ordered, sticky, sendingUser,
                            app.mState.getReportedProcState());
                // TODO: Uncomment this when (b/28322359) is fixed and we aren't getting
                // DeadObjectException when the process isn't actually dead.
                //} catch (DeadObjectException ex) {
                // Failed to call into the process.  It's dying so just let it die and move on.
                //    throw ex;
                } catch (RemoteException ex) {
                    // Failed to call into the process. It's either dying or wedged. Kill it gently.
                    synchronized (mService) {
                        Slog.w(TAG, "Can't deliver broadcast to " + app.processName
                                + " (pid " + app.getPid() + "). Crashing it.");
                        app.scheduleCrashLocked("can't deliver broadcast",
                                CannotDeliverBroadcastException.TYPE_ID, /* extras=*/ null);
                    }
                    throw ex;
                }
            } else {
                // Application has died. Receiver doesn't exist.
                throw new RemoteException("app.thread must not be null");
            }
        } else {
            receiver.performReceive(intent, resultCode, data, extras, ordered,
                    sticky, sendingUser);
        }
		..........
    }

这里如果广播接收者所在的应用程序进程存在且正在运行,就会调用注释一处的scheduleRegisteredReceiver方法,这个thread是IApplicationThread类型的,显然又是远程调用具体就是ApplicationThread,也就是ActivityThread,最后会用广播接收者所在的进程来接收广播,来看ApplicationThread的对应方法:

        public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
                int resultCode, String dataStr, Bundle extras, boolean ordered,
                boolean sticky, int sendingUser, int processState) throws RemoteException {
            updateProcessState(processState, false);
            receiver.performReceive(intent, resultCode, dataStr, extras, ordered,
                    sticky, sendingUser);
        }

这里又会调用到receiver的performReceive方法,这个receiver是IIntentReceiver 类型的,具体是会在LoadedApk的ReceiverDispatcher类的InnerReceiver实现的,我们看它的performReceive方法:

public void performReceive(Intent intent, int resultCode, String data,
                    Bundle extras, boolean ordered, boolean sticky, int sendingUser) {
                final LoadedApk.ReceiverDispatcher rd;
                if (intent == null) {
                    Log.wtf(TAG, "Null intent received");
                    rd = null;
                } else {
                    rd = mDispatcher.get();
                }
                if (ActivityThread.DEBUG_BROADCAST) {
                    int seq = intent.getIntExtra("seq", -1);
                    Slog.i(ActivityThread.TAG, "Receiving broadcast " + intent.getAction()
                            + " seq=" + seq + " to " + (rd != null ? rd.mReceiver : null));
                }
                if (rd != null) {
                    rd.performReceive(intent, resultCode, data, extras, //1------1
                            ordered, sticky, sendingUser);
                } else {
                    // The activity manager dispatched a broadcast to a registered
                    // receiver in this process, but before it could be delivered the
                    // receiver was unregistered.  Acknowledge the broadcast on its
                    // behalf so that the system's broadcast sequence can continue.
                    if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG,
                            "Finishing broadcast to unregistered receiver");
                    IActivityManager mgr = ActivityManager.getService();
                    try {
                        if (extras != null) {
                            extras.setAllowFds(false);
                        }
                        mgr.finishReceiver(this, resultCode, data, extras, false, intent.getFlags());
                    } catch (RemoteException e) {
                        throw e.rethrowFromSystemServer();
                    }
                }
            }
        }

来看注释一处,这里会调用到LoadedApk.ReceiverDispatcher的performReceive方法:

        public void performReceive(Intent intent, int resultCode, String data,
                Bundle extras, boolean ordered, boolean sticky, int sendingUser) {
            final Args args = new Args(intent, resultCode, data, extras, ordered,
                    sticky, sendingUser);  //1---------1
            if (intent == null) {
                Log.wtf(TAG, "Null intent received");
            } else {
                if (ActivityThread.DEBUG_BROADCAST) {
                    int seq = intent.getIntExtra("seq", -1);
                    Slog.i(ActivityThread.TAG, "Enqueueing broadcast " + intent.getAction()
                            + " seq=" + seq + " to " + mReceiver);
                }
            }
            if (intent == null || !mActivityThread.post(args.getRunnable())) {//2--------2
                if (mRegistered && ordered) {
                    IActivityManager mgr = ActivityManager.getService();
                    if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG,
                            "Finishing sync broadcast to " + mReceiver);
                    args.sendFinished(mgr);
                }
            }
        }

注释一处,将方法传入的一系列参数进行封装,形成一个Args对象,然后会在注释二处将args的runnable发送到ActivityThread的Handler中,那我们该关心的就是这个getRunnable方法返回了什么内容了:

public final Runnable getRunnable() {
                return () -> {
                    final BroadcastReceiver receiver = mReceiver;
                    final boolean ordered = mOrdered;

                    if (ActivityThread.DEBUG_BROADCAST) {
                        int seq = mCurIntent.getIntExtra("seq", -1);
                        Slog.i(ActivityThread.TAG, "Dispatching broadcast " + mCurIntent.getAction()
                                + " seq=" + seq + " to " + mReceiver);
                        Slog.i(ActivityThread.TAG, "  mRegistered=" + mRegistered
                                + " mOrderedHint=" + ordered);
                    }

                    final IActivityManager mgr = ActivityManager.getService();
                    final Intent intent = mCurIntent;
                    if (intent == null) {
                        Log.wtf(TAG, "Null intent being dispatched, mDispatched=" + mDispatched
                                + (mRunCalled ? ", run() has already been called" : ""));
                    }

                    mCurIntent = null;
                    mDispatched = true;
                    mRunCalled = true;
                    if (receiver == null || intent == null || mForgotten) {
                        if (mRegistered && ordered) {
                            if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG,
                                    "Finishing null broadcast to " + mReceiver);
                            sendFinished(mgr);
                        }
                        return;
                    }

                    if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
                        Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
                                "broadcastReceiveReg: " + intent.getAction());
                    }

                    try {
                        ClassLoader cl = mReceiver.getClass().getClassLoader();
                        intent.setExtrasClassLoader(cl);
                        // TODO: determine at registration time if caller is
                        // protecting themselves with signature permission
                        intent.prepareToEnterProcess(ActivityThread.isProtectedBroadcast(intent),
                                mContext.getAttributionSource());
                        setExtrasClassLoader(cl);
                        receiver.setPendingResult(this);
                        receiver.onReceive(mContext, intent);//1-------1
                    } catch (Exception e) {
                        if (mRegistered && ordered) {
                            if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG,
                                    "Finishing failed broadcast to " + mReceiver);
                            sendFinished(mgr);
                        }
                        if (mInstrumentation == null ||
                                !mInstrumentation.onException(mReceiver, e)) {
                            Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                            throw new RuntimeException(
                                    "Error receiving broadcast " + intent
                                            + " in " + mReceiver, e);
                        }
                    }

                    if (receiver.getPendingResult() != null) {
                        finish();
                    }
                    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                };
            }
        }

这里直接看注释一处,就会触发接收器的onReceive回调方法,从而广播接收器正式接收到了广播的消息。到这里为止,广播的接收过程也结束了。

总结

相比Activity和Service的工作过程,Broadcast的工作过程还是比较好懂的,主要就是以AMS为媒介,注册时将接收器注册进AMS中存储接收器的列表中;然后发送过程中也通过AMS来实现发送,调用其存储的接收器的onReceive方法,这样就实现了Broadcast的注册,发送和接收。

下面是一张简要的总结图,省略了大部分细节:

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/647145.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

第七十四天学习记录:高等数学:不定积分(宋浩板书)

给定一个函数f(x)&#xff0c;如果存在一个函数F(x)&#xff0c;满足F’(x)f(x)&#xff0c;则称F(x)是f(x)的一个原函数。我们通常用∫f(x)dx表示f(x)的不定积分&#xff0c;它表示的是所有原函数的集合&#xff0c;也就是说&#xff0c;∫f(x)dx就是f(x)的原函数集合。 不定…

Docker安装wordpress并配置数据库(超详细版)

Docker在线拉取安装wordpress并配置数据库 一、拉取wordpress镜像(默认最新)二、启动wordpress容器三、查看容器状态四、安装wordpress博客程序 如果您已经在 Docker 容器中分别安装了 WordPress 和 MySQL&#xff0c;并且想要让它们链接起来&#xff0c;可以按照以下步骤进行操…

什么是三极管的截止饱和放大

三极管可以工作在三个状态&#xff0c;分别是截止状态&#xff0c;饱和状态和放大状态。 当三级管BE之间的电压小于等于BE间的开启电压UON&#xff0c;并且CE间电压>BE间电压时 &#xff0c;三极管处于截止状态&#xff0c;一般硅管在0.7V左右&#xff0c;锗管在0.3V左右。…

数据库第三章(SQL)

目录 1.SQL语言 索引 1.SQL语言 sql语言是个非过程性语言 sql的特点 1.综合统一&#xff0c;把增删查改都统一了起来 2.高度非过程化&#xff0c;不关心过程 3.面向集合的操作方式 sql基本语法 drop是删除表 delete是删除表的某个元组 安全方面&#xff1a;grant授权 revo…

Java实训日志05

文章目录 八、项目开发实现步骤&#xff08;七&#xff09;创建数据访问接口实现类1、创建学校数据访问接口实现类&#xff08;1&#xff09;编写按标识符查询学校记录方法&#xff08;2&#xff09;编写更新学校记录方法 1_、测试学校数据访问接口实现类&#xff08;1&#xf…

在Nginx服务器如何安装SSL证书

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言提示&#xff1a;我用的是阿里云的产品&#xff0c;就以阿里云进行的&#xff1a; 一、下载SSL证书二、安装SSL证书 前言 提示&#xff1a;我用的是阿里云的产…

多元回归预测 | Matlab遗传算法(GA)优化核极限学习机回归,GA-KELM回归预测,多变量输入模型

文章目录 效果一览文章概述部分源码参考资料效果一览 文章概述 多元回归预测 | Matlab遗传算法(GA)优化核极限学习机回归,GA-KELM回归预测,多变量输入模型 评价指标包括:MAE、RMSE和R2等,代码质量极高,方便学习和替换数据。要求2018版本及以上。 部分源码 %% 清空环境变量…

设计模式(十六):行为型之责任链模式

设计模式系列文章 设计模式(一)&#xff1a;创建型之单例模式 设计模式(二、三)&#xff1a;创建型之工厂方法和抽象工厂模式 设计模式(四)&#xff1a;创建型之原型模式 设计模式(五)&#xff1a;创建型之建造者模式 设计模式(六)&#xff1a;结构型之代理模式 设计模式…

2.链表的实现:带哨兵

1.带哨兵的单链表的定义 /*** 单向链表----带哨兵*/ public class SinglyLinked {/*** 节点类* 数据域和地址域*/private static class Node {int value; //值Node next; // 指向下一个节点public Node() {}public Node(int value, Node next) {this.value value;this.next …

【linux】在Ubuntu20.04下录制屏幕为视频,并制作成gif动态图片

1、下载软件 录制屏幕软件 sudo apt install simplescreenrecorder视频转图片软件 sudo apt install mplayer图片合成gif动态图片 sudo apt install imagemagick2、录制屏幕 1)运行命令 simplescreenrecorder2)设置:区域、帧率 选择录制的区域; 设置帧率为10(或者更小…

3.vue 指令

3.10 v-text和v-html 更新 DOM 对象的 innerText / innerHTML 语法: v-text"vue数据变量" v-html"vue数据变量" 注意: 会覆盖插值表达式 v-text 把值当成普通字符串显示 v-html 把值当做 html 解析 <template><div><p v-text"str…

关于自动化测试框架pytest的Fixture固件

这篇文章主要介绍了关于自动化测试框架pytest的Fixture固件,Fixture它其实就是一些函数,会在执行测试方法/测试函数前后加载运行它们,需要的朋友可以参考下 目录 什么是固件定义方式调用方式使用fixture传递测试数据conftest.py作用域总结 什么是固件 Fixture 翻译成中文即是固…

无效目标发行版问题解决记录

​ SpringMVC之‘无效目标发行版’ 问题&#xff1a;上死亡截图 问题解决思路&#xff1a;这是由于JDK版本不匹配导致的错误。 首先说一下问题的关键所在&#xff0c;然后再细说解决步骤&#xff1a; 遇到这个问题的朋友大概率都是在写Spring项目时遇到的&#xff0c;这就需要M…

H3C-HCL模拟器常用命令及其操作演示

一、实验拓扑图 二、实验设备 设备1&#xff1a;路由器"MSR36-20"&#xff1b; 设备2&#xff1a;真机&#xff1b; 三、常用命令 1&#xff09;进入用户视图&#xff1a;启动默认就是用户视图 "< >" 2&#xff09;进入系统视图&#xff1a;"…

揭示GPT Tokenizer的工作原理

在GPT模型中&#xff0c;tokenization&#xff08;词元化&#xff09;指的是将用户输入的文本分割成token&#xff08;词元&#xff09;的过程&#xff0c;以让GPT能更好地理解输入文本的词义、句法和语义&#xff0c;以及生成更连贯的输出内容。这是非常重要的预处理操作&…

C++AVL树

目录&#xff1a; AVL树的概念AVL树节点的定义更新平衡因子 AVL树的旋转AVL树的验证AVL的整体实现 AVL树的删除AVL树的性能 总结 AVL树的概念 AVL树&#xff1a;二叉搜索树虽可以缩短查找的效率&#xff0c;但如果数据有序或接近有序二叉搜索树将退化为单支树&#xff0c;查找…

CAPL硬件控制课程,物理故障注入自动化以及程控电源

&#x1f4d9; CAN/LIN总线物理故障注入&#xff0c;自动化解决方案 车载网络通讯的健壮性和故障恢复能力至关重要&#xff0c;所以我们需要对控制器进行各种物理容错测试&#xff0c;常规情况下我们需要注入如下8种物理故障注入&#xff1a;CANH断路&#xff0c;CANL断路,CANH…

Redux的基础操作和思想

什么是Redux? Redux是JavaScript应用的状态容器&#xff0c;提供可预测的状态管理! Redux除了和React一起用外&#xff0c;还支持其它框架;它体小精悍(只有2kB&#xff0c; 包括依赖)&#xff0c;却有很强大的插件扩展生态! Redux提供的模式和工具使您更容易理解应用程序中的…

管理类联考——英语——趣味篇——完型填空

完型填空解题秘籍 一、答案分配规律 历年完型答案统计 A B C D 2010 DCBAA/BDCBA/CDDAC/BDCAB 5 5 5 5 2011 ACBDD/BACCB/DBACA/ADACD 6 4 5 5 2012 BBAAC/DAACB/DBCDD/ACCBD 5 5 5 5 2013 ADBDC/BBDBA/ADCCC/CABAD 5 5 5 5 2014 BACAD/ACCDB/ABCDB/DADCB 5 5 5 5 2015 CDC…

leetcode199. 二叉树的右视图(java)

二叉树的右视图 leetcode199. 二叉树的右视图题目描述 广度优先遍历二叉树专题 leetcode199. 二叉树的右视图 来源&#xff1a;力扣&#xff08;LeetCode&#xff09; 链接&#xff1a;https://leetcode.cn/problems/binary-tree-right-side-view 题目描述 给定一个二叉树的 根…