Android广播发送机制剖析【android广播系列二】

来源:互联网 发布:阿里云 宕机 经济损失 编辑:程序博客网 时间:2024/04/27 21:00
   上篇博客大致说了说广播的注册机制,动态注册和静态注册广播的原理还不一样,动态广播最后HashMap中了,最后放到mReceiverResolver中,   以后当ActivityManagerService接收到广播的时候,它就可以再成员变量mReceiverResolver中找到对应的广播接收者了。下面我们来说说广播的发送机制。   广播发送时候,在Context.java的方法中调用的,有如下方法:   sendBroadcast(Intent intent) ;   sendBroadcast(Intent intent, String receiverPermission)    sendBroadcastAsUser(Intent intent, UserHandle user)    sendBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission)   sendOrderedBroadcast(Intent intent, String receiverPermission)   sendOrderedBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)    sendStickyBroadcast(Intent intent)    sendStickyBroadcastAsUser(Intent intent, UserHandle user)    sendStickyOrderedBroadcast(Intent intent, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)    sendStickyOrderedBroadcastAsUser(Intent intent, UserHandle user, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)   以上就是发送广播的方式,但是常用的就几种,其中比较常用的就是sendBroadcast(Intent intent) ;我就说下这种发送广播的方式。

发送广播的大致流程

Step1:sendBroadcast()方法开始

sendBroadcast在context中调用的,往下调用到了ContextImpl.java中去了:

/** * Common implementation of Context API, which provides the base * context object for Activity and other application components. */class ContextImpl extends Context {    private final static String TAG = "ContextImpl";    private final static boolean DEBUG = false;
@Overridepublic void sendBroadcast(Intent intent, String receiverPermission) {        warnIfCallingFromSystemProcess();        String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());        try {            intent.prepareToLeaveProcess();            ActivityManagerNative.getDefault().broadcastIntent(                mMainThread.getApplicationThread(), intent, resolvedType, null,                Activity.RESULT_OK, null, null, receiverPermission, AppOpsManager.OP_NONE,                false, false, getUserId());        } catch (RemoteException e) {        }    }

Step2:调用到broadcastIntent()方法中

调用到了ActivityManagerService.java中的broadcastIntent()方法中去了,

public final int broadcastIntent(IApplicationThread caller,            Intent intent, String resolvedType, IIntentReceiver resultTo,int resultCode, String resultData, Bundle map, String requiredPermission, int appOp, 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();            int res = broadcastIntentLocked(callerApp,                    callerApp != null ? callerApp.info.packageName : null,                    intent, resolvedType, resultTo,                    resultCode, resultData, map, requiredPermission, appOp, serialized, sticky,                    callingPid, callingUid, userId);            Binder.restoreCallingIdentity(origId);            return res;        }    }

Step3:调用到broadcastIntentLocked()方法中

这个方法有点复杂,逻辑量有点大

private final int broadcastIntentLocked(ProcessRecord callerApp, String callerPackage, Intent intent, String resolvedType, IIntentReceiver resultTo, int resultCode, String resultData, Bundle map, String requiredPermission, int appOp, boolean ordered, boolean sticky, int callingPid, int callingUid, int userId) {        intent = new Intent(intent);        // By default broadcasts do not go to stopped apps.1)如果apk被forceStop()强行停止或者安装后就没有启动过,这个apk就处于停 止状态(stopped state)===============================================================intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES);        if (DEBUG_BROADCAST_LIGHT) Slog.v(            TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent            + " ordered=" + ordered + " userid=" + userId);        if ((resultTo != null) && !ordered) {            Slog.w(TAG, "Broadcast " + intent + " not ordered but result callback requested!");        }        userId = handleIncomingUser(callingPid, callingUid, userId,                true, ALLOW_NON_FULL, "broadcast", callerPackage);        // Make sure that the user who is receiving this broadcast is started.        // If not, we will just skip it.        if (userId != UserHandle.USER_ALL && mStartedUsers.get(userId) == null) {            if (callingUid != Process.SYSTEM_UID || (intent.getFlags() & Intent.FLAG_RECEIVER_BOOT_UPGRADE) == 0){    Slog.w(TAG, "Skipping broadcast of " + intent + ": user " + userId + " is stopped");                return ActivityManager.BROADCAST_SUCCESS;            }        }2)判断当前是否有权力发出广播===============        /*         * Prevent non-system code (defined here to be non-persistent         * processes) from sending protected broadcasts.         */        int callingAppId = UserHandle.getAppId(callingUid);        if (callingAppId == Process.SYSTEM_UID || callingAppId == Process.PHONE_UID            || callingAppId == Process.SHELL_UID || callingAppId == Process.BLUETOOTH_UID            || callingAppId == Process.NFC_UID || callingUid == 0) {            // Always okay.        } else if (callerApp == null || !callerApp.persistent) {            try {          if (AppGlobals.getPackageManager().isProtectedBroadcast(                        intent.getAction())) {                    String msg = "Permission Denial: not allowed to send broadcast " + intent.getAction() + " from pid=" + callingPid + ", uid=" + callingUid;                    Slog.w(TAG, msg);                    throw new SecurityException(msg);                } else if (AppWidgetManager.ACTION_APPWIDGET_CONFIGURE.equals(intent.getAction())) {                    // Special case for compatibility: we don't want apps to send this,                    // but historically it has not been protected and apps may be using it                    // to poke their own app widget.  So, instead of making it protected,                    // just limit it to the caller.                    if (callerApp == null) {                        String msg = "Permission Denial: not allowed to send broadcast "                                + intent.getAction() + " from unknown caller.";                        Slog.w(TAG, msg);                        throw new SecurityException(msg);                    } else if (intent.getComponent() != null) {                        // They are good enough to send to an explicit component...  verify                        // it is being sent to the calling app.                if (!intent.getComponent().getPackageName().equals(                                callerApp.info.packageName)) {                            String msg = "Permission Denial: not allowed to send broadcast " + intent.getAction() + " to "   +intent.getComponent().getPackageName() + " from "callerApp.info.packageName;                            Slog.w(TAG, msg);                            throw new SecurityException(msg);                        }                    } else {                        // Limit broadcast to their own package.                   intent.setPackage(callerApp.info.packageName);                    }                }            } catch (RemoteException e) {                Slog.w(TAG, "Remote exception", e);                return ActivityManager.BROADCAST_SUCCESS;            }        }3)处理和package相关的广播,如果这个package被卸载了,我们也要相应地移除它所有的组件=================================================        // Handle special intents: if this broadcast is from the package        // manager about a package being removed, we need to remove all of        // its activities from the history stack.        final boolean uidRemoved = Intent.ACTION_UID_REMOVED.equals(                intent.getAction());        if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())                || Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())                || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())                || Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())                || uidRemoved) {            if (checkComponentPermission(                    android.Manifest.permission.BROADCAST_PACKAGE_REMOVED,                    callingPid, callingUid, -1, true)                    == PackageManager.PERMISSION_GRANTED) {                if (uidRemoved) {                    final Bundle intentExtras = intent.getExtras();                    final int uid = intentExtras != null              ? intentExtras.getInt(Intent.EXTRA_UID) : -1;                    if (uid >= 0) {                        BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics();                        synchronized (bs) {                            bs.removeUidStatsLocked(uid);                        }                        mAppOpsService.uidRemoved(uid);                    }                } else {                    // If resources are unavailable just force stop all                    // those packages and flush the attribute cache as well.                       if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) {                        String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);                   if (list != null && (list.length > 0)) {                            for (String pkg : list) {                                forceStopPackageLocked(pkg, -1, false, true, true, false, false, userId,                                        "storage unmount");                            }                            cleanupRecentTasksLocked(UserHandle.USER_ALL);                            sendPackageBroadcastLocked(                                    IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list, userId);                        }                    } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(                            intent.getAction())) {                        cleanupRecentTasksLocked(UserHandle.USER_ALL);                    } else {                        Uri data = intent.getData();                        String ssp;                        if (data != null && (ssp=data.getSchemeSpecificPart()) != null) {                            boolean removed = Intent.ACTION_PACKAGE_REMOVED.equals(                                    intent.getAction());                            boolean fullUninstall = removed &&                                    !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);                            if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) {                                forceStopPackageLocked(ssp, UserHandle.getAppId(                                        intent.getIntExtra(Intent.EXTRA_UID, -1)), false, true, true,                                        false, fullUninstall, userId,                                        removed ? "pkg removed" : "pkg changed");                            }                            if (removed) {                                sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED,                                        new String[] {ssp}, userId);                                if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {                                    mAppOpsService.packageRemoved(                                            intent.getIntExtra(Intent.EXTRA_UID, -1), ssp); // Remove all permissions granted from/to this package                                  removeUriPermissionsForPackageLocked(ssp, userId, true);                                }                            }                        }                    }                }            } else {                String msg = "Permission Denial: " + intent.getAction()                        + " broadcast from " + callerPackage + " (pid=" + callingPid                        + ", uid=" + callingUid + ")"                        + " requires "                        + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED;                Slog.w(TAG, msg);                throw new SecurityException(msg);            }        // Special case for adding a package: by default turn on compatibility        // mode.        } else if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {            Uri data = intent.getData();            String ssp;            if (data != null && (ssp=data.getSchemeSpecificPart()) != null) {                mCompatModePackages.handlePackageAddedLocked(ssp,                        intent.getBooleanExtra(Intent.EXTRA_REPLACING, false));            }        }4)处理其他一些系统广播ACTION_TIMEZONE_CHANGED,ACTION_TIME_CHANGED,PROXY_CHANGE_ACTION,========================================================================        /*         * If this is the time zone changed action, queue up a message that will reset the timezone         * of all currently running processes. This message will get queued up before the broadcast         * happens.         */        if (Intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) {            mHandler.sendEmptyMessage(UPDATE_TIME_ZONE);        }        /*         * If the user set the time, let all running processes know.         */        if (Intent.ACTION_TIME_CHANGED.equals(intent.getAction())) {            final int is24Hour = intent.getBooleanExtra(                    Intent.EXTRA_TIME_PREF_24_HOUR_FORMAT, false) ? 1 : 0;            mHandler.sendMessage(mHandler.obtainMessage(UPDATE_TIME, is24Hour, 0));            BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();            synchronized (stats) {                stats.noteCurrentTimeChangedLocked();            }        }        if (Intent.ACTION_CLEAR_DNS_CACHE.equals(intent.getAction())) {            mHandler.sendEmptyMessage(CLEAR_DNS_CACHE_MSG);        }        if (Proxy.PROXY_CHANGE_ACTION.equals(intent.getAction())) {            ProxyInfo proxy = intent.getParcelableExtra(Proxy.EXTRA_PROXY_INFO);            mHandler.sendMessage(mHandler.obtainMessage(UPDATE_HTTP_PROXY_MSG, proxy));        }5)判断广播是否是一个黏性广播,所有相同类型的黏性广播保存起到一个列表中,最后会保存到mStickyBroadcasts这个HashMap中。======================================        // Add to the sticky list if requested.        if (sticky) {            if (checkPermission(android.Manifest.permission.BROADCAST_STICKY,                    callingPid, callingUid)                    != PackageManager.PERMISSION_GRANTED) {                String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid="                        + callingPid + ", uid=" + callingUid                        + " requires " + android.Manifest.permission.BROADCAST_STICKY;                Slog.w(TAG, msg);                throw new SecurityException(msg);            }            if (requiredPermission != null) {                Slog.w(TAG, "Can't broadcast sticky intent " + intent                        + " and enforce permission " + requiredPermission);                return ActivityManager.BROADCAST_STICKY_CANT_HAVE_PERMISSION;            }            if (intent.getComponent() != null) {                throw new SecurityException(                        "Sticky broadcasts can't target a specific component");            }            // We use userId directly here, since the "all" target is maintained            // as a separate set of sticky broadcasts.            if (userId != UserHandle.USER_ALL) {                // But first, if this is not a broadcast to all users, then                // make sure it doesn't conflict with an existing broadcast to                // all users.                ArrayMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(                        UserHandle.USER_ALL);                if (stickies != null) {                    ArrayList<Intent> list = stickies.get(intent.getAction());                    if (list != null) {                        int N = list.size();                        int i;                        for (i=0; i<N; i++) {                            if (intent.filterEquals(list.get(i))) {                                throw new IllegalArgumentException( "Sticky broadcast " + intent + " for user " + userId + " conflicts with existing global broadcast");                            }                        }                    }                }            }            ArrayMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId);            if (stickies == null) {                stickies = new ArrayMap<String, ArrayList<Intent>>();                mStickyBroadcasts.put(userId, stickies);            }            ArrayList<Intent> list = stickies.get(intent.getAction());            if (list == null) {                list = new ArrayList<Intent>();                stickies.put(intent.getAction(), list);            }            int N = list.size();            int i;            for (i=0; i<N; i++) {                if (intent.filterEquals(list.get(i))) {                    // This sticky already exists, replace it.                    list.set(i, new Intent(intent));                    break;                }            }            if (i >= N) {                list.add(new Intent(intent));            }        }        int[] users;        if (userId == UserHandle.USER_ALL) {            // Caller wants broadcast to go to all started users.            users = mStartedUserArray;        } else {            // Caller wants broadcast to go to one specific user.            users = new int[] {userId};        }6)先查询静态广播保存到receivers中,再查询动态广播保存到registeredReceivers中=====================================================        //找出谁都会收到这个广播。        // Figure out who all will receive this broadcast.        List receivers = null;        List<BroadcastFilter> registeredReceivers = null;// Need to resolve the intent to interested receivers...//如果下面的if语句为true,表示找到所有静态的广播保存到receivers 中        if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY)                 == 0) {            receivers = collectReceiverComponents(intent, resolvedType, callingUid, users);        }   //如果if:true,则queryIntent查询所有匹配intent的Receiver;        if (intent.getComponent() == null) {            if (userId == UserHandle.USER_ALL && callingUid == Process.SHELL_UID) {                // Query one target user at a time, excluding shell-restricted users                UserManagerService ums = getUserManagerLocked();                for (int i = 0; i < users.length; i++) {                    if (ums.hasUserRestriction(                            UserManager.DISALLOW_DEBUGGING_FEATURES, users[i])) {                        continue;                    }                    List<BroadcastFilter> registeredReceiversForUser =                            mReceiverResolver.queryIntent(intent,                                    resolvedType, false, users[i]);                    if (registeredReceivers == null) {                        registeredReceivers = registeredReceiversForUser;                    } else if (registeredReceiversForUser != null) {   // 表示找到所有动态的广播保存到registeredReceivers中                     registeredReceivers.addAll(registeredReceiversForUser);                    }                }            } else {                registeredReceivers = mReceiverResolver.queryIntent(intent,                        resolvedType, false, userId);            }        }7)广播的调度,以及广播的分发=======//是否替换上一个相同的广播,即:上次接受的一个广播还未来得及转发给接收者,//又马上发送了一个相同的广播。这这种情况下接受的getFlags()==1,//表示要替换旧的广播,即replacePending = true;   final boolean replacePending =             (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0;        if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction()                + " replacePending=" + replacePending);        int NR = registeredReceivers != null ? registeredReceivers.size() : 0;  //ordered表示是否是一个无序的广播,并且是registeredReceivers动态广播  //这就说明动态广播要优先于静态广播的注册者        if (!ordered && NR > 0) {// If we are not serializing this broadcast, then send the// registered receivers separately so they don't wait for the// components to be launched.            final BroadcastQueue queue = broadcastQueueForIntent(intent);            BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, callerPackage, callingPid, callingUid, resolvedType, requiredPermission, appOp, registeredReceivers, resultTo, resultCode, resultData, map, ordered, sticky, false, userId);            if (DEBUG_BROADCAST) Slog.v(                    TAG, "Enqueueing parallel broadcast " + r);            final boolean replaced = replacePending && queue.replaceParallelBroadcastLocked(r);            if (!replaced) {//这个做了修改,把无序广播的调度队列封装到BroadcastQueue.java这个类中//去了,mParallelBroadcasts来表示                queue.enqueueParallelBroadcastLocked(r);                queue.scheduleBroadcastsLocked();            }            registeredReceivers = null;            NR = 0;        }        // Merge into one list.        int ir = 0;        if (receivers != null) {// A special case for PACKAGE_ADDED: do not allow the package// being added to see this broadcast.  This prevents them from// using this as a back door to get run as soon as they are// installed.  Maybe in the future we want to have a special install// broadcast or such for apps, but we'd like to deliberately make// this decision.8)合并静态广播receivers和静态广播registerReceivers列表为一个列表=======            String skipPackages[] = null;            if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())                    || Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())                    || Intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) {                Uri data = intent.getData();                if (data != null) {                    String pkgName = data.getSchemeSpecificPart();                    if (pkgName != null) {                        skipPackages = new String[] { pkgName };                    }                }            } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) {                skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);            }            if (skipPackages != null && (skipPackages.length > 0)) {                for (String skipPackage : skipPackages) {                    if (skipPackage != null) {                        int NT = receivers.size();                        for (int it=0; it<NT; it++) {                            ResolveInfo curt = (ResolveInfo)receivers.get(it);                            if (curt.activityInfo.packageName.equals(skipPackage)) {                                receivers.remove(it);                                it--;                                NT--;                            }                        }                    }                }            }            int NT = receivers != null ? receivers.size() : 0;            int it = 0;            ResolveInfo curt = null;            BroadcastFilter curr = null;//合并动态广播和静态广播,合并前它们都是按照优先级排好序的,//根据优先级来进行合并的算法@{            while (it < NT && ir < NR) {                if (curt == null) {                    curt = (ResolveInfo)receivers.get(it);                }                if (curr == null) {                    curr = registeredReceivers.get(ir);                }                if (curr.getPriority() >= curt.priority) {                    // Insert this broadcast record into the final list.                    receivers.add(it, curr);                    ir++;                    curr = null;                    it++;                    NT++;                } else {                    // Skip to the next ResolveInfo in the final list.                    it++;                    curt = null;                }            }            //合并动态广播和静态广播,根据优先级来进行合并的算法@}        } //如果没有完全把registeredReceivers列表中的数据合并到receivers中,//就用while循环依次添加到recivers的末尾,因为它们是优先级最低的广播接收者        while (ir < NR) {            if (receivers == null) {                receivers = new ArrayList();            }            receivers.add(registeredReceivers.get(ir));            ir++;        }        if ((receivers != null && receivers.size() > 0)                || resultTo != null) {            BroadcastQueue queue = broadcastQueueForIntent(intent);            BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp,                    callerPackage, callingPid, callingUid, resolvedType,                    requiredPermission, appOp, receivers, resultTo, resultCode,                    resultData, map, ordered, sticky, false, userId);            if (DEBUG_BROADCAST) Slog.v(                    TAG, "Enqueueing ordered broadcast " + r                    + ": prev had " + queue.mOrderedBroadcasts.size());            if (DEBUG_BROADCAST) {                int seq = r.intent.getIntExtra("seq", -1);                Slog.i(TAG, "Enqueueing broadcast " + r.intent.getAction() + " seq=" + seq);            }            boolean replaced = replacePending && queue.replaceOrderedBroadcastLocked(r);             if (!replaced) {  //广播的分发,真正发送广播的地方在BroadcastQueue.java中执行的                queue.enqueueOrderedBroadcastLocked(r);                queue.scheduleBroadcastsLocked();            }        }        return ActivityManager.BROADCAST_SUCCESS;    }

Step4:接着来看看scheduleBroadcastsLocked()方法

public void scheduleBroadcastsLocked() {     if (DEBUG_BROADCAST) Slog.v(TAG, "Schedule broadcasts ["                + mQueueName + "]: current="                + mBroadcastsScheduled);        if (mBroadcastsScheduled) {            return;        }//这个方法比较简单,就发送了一个消息 BROADCAST_INTENT_MSG //从这个方法可以看出广播的发送和接收是异步的,不是同步进行的。   mHandler.sendMessage(mHandler.obtainMessage(BROADCAST_INTENT_MSG, this));        mBroadcastsScheduled = true;    }

Step5:执行消息BROADCAST_INTENT_MSG的地方:

private final class BroadcastHandler extends Handler {        public BroadcastHandler(Looper looper) {            super(looper, null, true);        }        @Override        public void handleMessage(Message msg) {            switch (msg.what) {                case BROADCAST_INTENT_MSG: {                    if (DEBUG_BROADCAST) Slog.v(                            TAG, "Received BROADCAST_INTENT_MSG");      //processNextBroadcast这个方法很重要,是分发广播的地方                    processNextBroadcast(true);                } break;                case BROADCAST_TIMEOUT_MSG: {                    synchronized (mService) {                        broadcastTimeoutLocked(true);                    }                } break;            }        }    };

以上方法执行processNextBroadcast(true);这个是最重要的调用广播发送的方法了。

Step6:processNextBroadcast()方法的执行:

final void processNextBroadcast(boolean fromMsg) {        synchronized(mService) {            BroadcastRecord r; if (DEBUG_BROADCAST) Slog.v(TAG, "processNextBroadcast ["                    + mQueueName + "]: "                    + mParallelBroadcasts.size() + " broadcasts, " + mOrderedBroadcasts.size() + " ordered broadcasts");            mService.updateCpuStats();            if (fromMsg) {   //这个表示前面的Scheduled还没有执行,handleMessage中会接着处理                mBroadcastsScheduled = false;            } // First, deliver any non-serialized broadcasts right away. //mParallelBroadcasts这个就是一个无序的广播调度队列            while (mParallelBroadcasts.size() > 0) {                r = mParallelBroadcasts.remove(0);                r.dispatchTime = SystemClock.uptimeMillis();                r.dispatchClockTime = System.currentTimeMillis();                final int N = r.receivers.size();                if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing parallel broadcast ["                        + mQueueName + "] " + r);                for (int i=0; i<N; i++) {                    Object target = r.receivers.get(i);              if (DEBUG_BROADCAST)  Slog.v(TAG,                            "Delivering non-ordered on [" + mQueueName + "] to registered "                            + target + ": " + r);              //调用无序广播,将无需广播发送给每一个注册者                    deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false);                }                addBroadcastToHistoryLocked(r);                if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Done with parallel broadcast ["                        + mQueueName + "] " + r);            }            // Now take care of the next serialized one...// If we are waiting for a process to come up to handle the next// broadcast, then do nothing at this point.  Just in case, we// check that the process we're waiting for still exists.            if (mPendingBroadcast != null) {                if (DEBUG_BROADCAST_LIGHT) {                    Slog.v(TAG, "processNextBroadcast ["                            + mQueueName + "]: waiting for "                            + mPendingBroadcast.curApp);                }                boolean isDead;                synchronized (mService.mPidsSelfLocked) {                    ProcessRecord proc = mService.mPidsSelfLocked.get(mPendingBroadcast.curApp.pid);                    isDead = proc == null || proc.crashing;                }                //如果它运行中,保持等待                if (!isDead) {                    // It's still alive, so keep waiting                    return;                } else {       Slog.w(TAG, "pending app  [" + mQueueName + "]" + mPendingBroadcast.curApp + " died before responding to broadcast");                    mPendingBroadcast.state = BroadcastRecord.IDLE;  //如果它没有运行,就准备给它发送一个广播mPendingBroadcastRecvIndex                    mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex;                    mPendingBroadcast = null;                }            }            boolean looped = false;    //有序广播mOrderedBroadcasts的调度任务的处理            do {                if (mOrderedBroadcasts.size() == 0) {                    // No more broadcasts pending, so all done!                    mService.scheduleAppGcsLocked();                    if (looped) {                        // If we had finished the last ordered broadcast, then                        // make sure all processes have correct oom and sched                        // adjustments.                        mService.updateOomAdjLocked();                    }                    return;                }                r = mOrderedBroadcasts.get(0);                boolean forceReceive = false;                // Ensure that even if something goes awry with the timeout                // detection, we catch "hung" broadcasts here, discard them,                // and continue to make progress.                //                // This is only done if the system is ready so that PRE_BOOT_COMPLETED                // receivers don't get executed with timeouts. They're intended for                // one time heavy lifting after system upgrades and can take                // significant amounts of time.                int numReceivers = (r.receivers != null) ? r.receivers.size() : 0;                if (mService.mProcessesReady && r.dispatchTime > 0) {                    long now = SystemClock.uptimeMillis();            //检查广播的的处理时间是否在规定的时间完成                    if ((numReceivers > 0) &&                            (now > r.dispatchTime + (2*mTimeoutPeriod*numReceivers))) {   Slog.w(TAG, "Hung broadcast [" + mQueueName + "] discarded after timeout failure:"+ " now=" + now + " dispatchTime=" + r.dispatchTime + " startTime=" + r.receiverTime + " intent=" + r.intent + " numReceivers=" + numReceivers + " nextReceiver=" + r.nextReceiver  + " state=" + r.state);   //强制结束这个广播转发任务,并且把广播的状态置为IDLE状态;                        broadcastTimeoutLocked(false);               // forcibly finish this broadcast                        forceReceive = true;                        r.state = BroadcastRecord.IDLE;                    }                }                if (r.state != BroadcastRecord.IDLE) {                    if (DEBUG_BROADCAST) Slog.d(TAG,                            "processNextBroadcast("                            + mQueueName + ") called when not idle (state="                            + r.state + ")");                    return;                }                if (r.receivers == null || r.nextReceiver >= numReceivers                        || r.resultAbort || forceReceive) {                    // No more receivers for this broadcast!  Send the final                    // result if requested...                    if (r.resultTo != null) {                        try {                            if (DEBUG_BROADCAST) {                                int seq = r.intent.getIntExtra("seq", -1);                                Slog.i(TAG, "Finishing broadcast ["                                        + mQueueName + "] " + r.intent.getAction()                                        + " seq=" + seq + " app=" + r.callerApp);                                        + performReceiveLocked(r.callerApp, r.resultTo,         new Intent(r.intent), r.resultCode, r.resultData,                    r.resultExtras, false, false, r.userId);   // Set this to null so that the reference   // (local and remote) isn't kept in the mBroadcastHistory.                            r.resultTo = null;                        } catch (RemoteException e) {                            r.resultTo = null;                            Slog.w(TAG, "Failure ["                                    + mQueueName + "] sending broadcast result of "                                    + r.intent, e);                        }                    }                    if (DEBUG_BROADCAST) Slog.v(TAG, "Cancelling BROADCAST_TIMEOUT_MSG"); //取消广播删除BROADCAST_TIMEOUT_MSG这个消息                    cancelBroadcastTimeoutLocked();      if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Finished with ordered broadcast " + r);                    // ... and on to the next...                    addBroadcastToHistoryLocked(r);                    mOrderedBroadcasts.remove(0);                    r = null;                    looped = true;                    continue;                }            } while (r == null);            // Get the next receiver...            //获取下一个广播            int recIdx = r.nextReceiver++;// Keep track of when this receiver started, and make sure there// is a timeout message pending to kill it if need be.            r.receiverTime = SystemClock.uptimeMillis();            if (recIdx == 0) {                r.dispatchTime = r.receiverTime;                r.dispatchClockTime = System.currentTimeMillis();                if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing ordered broadcast ["                        + mQueueName + "] " + r);            }  //检查是否已经发送过BROADCAST_TIMEOUT_MSG这个消息了            if (!mPendingBroadcastTimeoutMessage) {                long timeoutTime = r.receiverTime + mTimeoutPeriod;                if (DEBUG_BROADCAST) Slog.v(TAG,                        "Submitting BROADCAST_TIMEOUT_MSG ["                        + mQueueName + "] for " + r + " at " + timeoutTime);               //如果没有发送,现在就发送                setBroadcastTimeoutLocked(timeoutTime);            }            Object nextReceiver = r.receivers.get(recIdx);            if (nextReceiver instanceof BroadcastFilter) {                // Simple case: this is a registered receiver who gets                // a direct call.//把nextReceiver强制转换为BroadcastFilter表示是动态广播                BroadcastFilter filter = (BroadcastFilter)nextReceiver;                if (DEBUG_BROADCAST)  Slog.v(TAG,                        "Delivering ordered ["                        + mQueueName + "] to registered "                        + filter + ": " + r);//直接发送广播,因为动态广播标明已经启动了,无需像静态广播那样判断是否已经启动   deliverToRegisteredReceiverLocked(r, filter, r.ordered);   //检查是否是无序的广播                if (r.receiver == null || !r.ordered) {      // The receiver has already finished, so schedule to                    // process the next one.                    if (DEBUG_BROADCAST) Slog.v(TAG, "Quick finishing ["                            + mQueueName + "]: ordered="                            + r.ordered + " receiver=" + r.receiver);                   //将状态置为idle空闲                    r.state = BroadcastRecord.IDLE;                    //结束广播的调度                    scheduleBroadcastsLocked();                }                return;            }// Hard case: need to instantiate the receiver, possibly// starting its application process to host it.//如果nextReceiver不是BroadcastFilter这种类型,而是ResolveInfo这种类型的,//就表示是静态广播            ResolveInfo info =                (ResolveInfo)nextReceiver;            ComponentName component = new ComponentName(                    info.activityInfo.applicationInfo.packageName,                    info.activityInfo.name);            boolean skip = false;            int perm = mService.checkComponentPermission(info.activityInfo.permission,                    r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid,                    info.activityInfo.exported);            if (perm != PackageManager.PERMISSION_GRANTED) {                if (!info.activityInfo.exported) {          Slog.w(TAG, "Permission Denial: broadcasting "                            + r.intent.toString()                            + " from " + r.callerPackage + " (pid=" + r.callingPid                            + ", uid=" + r.callingUid + ")"                            + " is not exported from uid " + info.activityInfo.applicationInfo.uid                            + " due to receiver " + component.flattenToShortString());                } else {       Slog.w(TAG, "Permission Denial: broadcasting "             + r.intent.toString() + " from " +   r.callerPackage + " (pid=" + r.callingPid                            + ", uid=" + r.callingUid + ")"                            + " requires " + info.activityInfo.permission  + " due to receiver " + component.flattenToShortString());                }                skip = true;            }            if (info.activityInfo.applicationInfo.uid != Process.SYSTEM_UID &&                r.requiredPermission != null) {                try {                    perm = AppGlobals.getPackageManager().                            checkPermission(r.requiredPermission,                                    info.activityInfo.applicationInfo.packageName);                } catch (RemoteException e) {                    perm = PackageManager.PERMISSION_DENIED;                }                if (perm != PackageManager.PERMISSION_GRANTED) {       Slog.w(TAG, "Permission Denial: receiving "                            + r.intent + " to " + component.flattenToShortString() + " requires " + r.requiredPermission + " due to sender " + r.callerPackage                            + " (uid " + r.callingUid + ")");                    skip = true;                }            }   //校验权限,看是否禁用了这个广播的权限!=MODE_ALLOWED            if (r.appOp != AppOpsManager.OP_NONE) {                int mode = mService.mAppOpsService.noteOperation(r.appOp,                        info.activityInfo.applicationInfo.uid, info.activityInfo.packageName);                if (mode != AppOpsManager.MODE_ALLOWED) {                    if (DEBUG_BROADCAST)  Slog.v(TAG,                            "App op " + r.appOp + " not allowed for broadcast to uid "                            + info.activityInfo.applicationInfo.uid + " pkg "                            + info.activityInfo.packageName);                    skip = true;                }            }            if (!skip) {                skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,                        r.callingPid, r.resolvedType, info.activityInfo.applicationInfo.uid);            }            boolean isSingleton = false;            try {                isSingleton = mService.isSingleton(info.activityInfo.processName,                        info.activityInfo.applicationInfo,                        info.activityInfo.name, info.activityInfo.flags);            } catch (SecurityException e) {                Slog.w(TAG, e.getMessage());                skip = true;            }            if ((info.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) {                if (ActivityManager.checkUidPermission(                        android.Manifest.permission.INTERACT_ACROSS_USERS,                        info.activityInfo.applicationInfo.uid)                                != PackageManager.PERMISSION_GRANTED) {                    Slog.w(TAG, "Permission Denial: Receiver " + component.flattenToShortString()                            + " requests FLAG_SINGLE_USER, but app does not hold "                            + android.Manifest.permission.INTERACT_ACROSS_USERS);                    skip = true;                }            }            if (r.curApp != null && r.curApp.crashing) {                // If the target process is crashing, just skip it.                Slog.w(TAG, "Skipping deliver ordered [" + mQueueName + "] " + r                        + " to " + r.curApp + ": process crashing");                skip = true;            }            if (!skip) {                boolean isAvailable = false;                try {                    isAvailable = AppGlobals.getPackageManager().isPackageAvailable(                            info.activityInfo.packageName,                            UserHandle.getUserId(info.activityInfo.applicationInfo.uid));                } catch (Exception e) {                    // all such failures mean we skip this receiver                    Slog.w(TAG, "Exception getting recipient info for "                            + info.activityInfo.packageName, e);                }                if (!isAvailable) {                    if (DEBUG_BROADCAST) {                        Slog.v(TAG, "Skipping delivery to " + info.activityInfo.packageName                                + " / " + info.activityInfo.applicationInfo.uid                                + " : package no longer available");                    }                    skip = true;                }            }            if (skip) {                if (DEBUG_BROADCAST)  Slog.v(TAG,                        "Skipping delivery of ordered ["                        + mQueueName + "] " + r + " for whatever reason");                r.receiver = null;                r.curFilter = null;                r.state = BroadcastRecord.IDLE;                scheduleBroadcastsLocked();                return;            }            r.state = BroadcastRecord.APP_RECEIVE;            //获取进程的名字            String targetProcess = info.activityInfo.processName;            r.curComponent = component;            final int receiverUid = info.activityInfo.applicationInfo.uid;            // If it's a singleton, it needs to be the same app or a special app            if (r.callingUid != Process.SYSTEM_UID && isSingleton                    && mService.isValidSingletonCall(r.callingUid, receiverUid)) {                info.activityInfo = mService.getActivityInfoForUser(info.activityInfo, 0);            }            r.curReceiver = info.activityInfo;            if (DEBUG_MU && r.callingUid > UserHandle.PER_USER_RANGE) {                Slog.v(TAG_MU, "Updated broadcast record activity info for secondary user, "                        + info.activityInfo + ", callingUid = " + r.callingUid + ", uid = "                        + info.activityInfo.applicationInfo.uid);            }//广播已经开始回调,不能设置这个package为stopped的状态 // Broadcast is being executed, its package can't be stopped.            try {                AppGlobals.getPackageManager().setPackageStoppedState(                        r.curComponent.getPackageName(), false, UserHandle.getUserId(r.callingUid));            } catch (RemoteException e) {            } catch (IllegalArgumentException e) {                Slog.w(TAG, "Failed trying to unstop package "                        + r.curComponent.getPackageName() + ": " + e);            }  // Is this receiver's application already running?            ProcessRecord app = mService.getProcessRecordLocked(targetProcess,                    info.activityInfo.applicationInfo.uid, false);          //调用静态广播,我们要先找到receiver所从属的进程的进程名。          //if语句为true,则目标进程已经存在了,已经在运行            if (app != null && app.thread != null) {                try {                    app.addPackage(info.activityInfo.packageName,                            info.activityInfo.applicationInfo.versionCode, mService.mProcessStats);                    //则调用processCurBroadcastLocked                    processCurBroadcastLocked(r, app);                    return;                } catch (RemoteException e) {                    Slog.w(TAG, "Exception when sending broadcast to "                          + r.curComponent, e);                } catch (RuntimeException e) {                    Slog.wtf(TAG, "Failed sending broadcast to "                            + r.curComponent + " with " + r.intent, e);// If some unexpected exception happened, just skip// this broadcast.  At this point we are not in the call// from a client, so throwing an exception out from here// will crash the entire system instead of just whoever// sent the broadcast.                    logBroadcastReceiverDiscardLocked(r);                    finishReceiverLocked(r, r.resultCode, r.resultData,                    //结束广播的调度                    scheduleBroadcastsLocked();                    // We need to reset the state if we failed to start the receiver.                    r.state = BroadcastRecord.IDLE;                    return;                }                // If a dead object exception was thrown -- fall through to                // restart the application.            }            // Not running -- get it started, to be executed when the app comes up.            //如果没有运行,就运行这个recevier            if (DEBUG_BROADCAST)  Slog.v(TAG,                    "Need to start app ["                    + mQueueName + "] " + targetProcess + " for broadcast " + r);            if ((SystemProperties.getInt("sys.quickboot.enable", 0) == 1 &&                        SystemProperties.getInt("sys.quickboot.poweron", 0) == 0 &&                       !getWhiteList().contains(info.activityInfo.applicationInfo.packageName))                || (r.curApp=mService.startProcessLocked(targetProcess,                    info.activityInfo.applicationInfo, true,                    r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND,                    "broadcast", r.curComponent,                    (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0, false, false))                            == null) {                // Ah, this recipient is unavailable.  Finish it if necessary,                // and mark the broadcast record as ready for the next.                Slog.w(TAG, "Unable to launch app "                        + info.activityInfo.applicationInfo.packageName + "/"                        + info.activityInfo.applicationInfo.uid + " for broadcast "                        + r.intent + ": process is bad");                logBroadcastReceiverDiscardLocked(r);                finishReceiverLocked(r, r.resultCode, r.resultData,                        r.resultExtras, r.resultAbort, false);                scheduleBroadcastsLocked();                r.state = BroadcastRecord.IDLE;                return;            }            mPendingBroadcast = r;            mPendingBroadcastRecvIndex = recIdx;        }    }

Step7:processCurBroadcastLocked()方法的调用

private final void processCurBroadcastLocked(BroadcastRecord r,            ProcessRecord app) throws RemoteException {        if (DEBUG_BROADCAST)  Slog.v(TAG,                "Process cur broadcast " + r + " for app " + app);        if (app.thread == null) {            throw new RemoteException();        }        r.receiver = app.thread.asBinder();        r.curApp = app;        app.curReceiver = r;        app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_RECEIVER);        mService.updateLruProcessLocked(app, false, null);        mService.updateOomAdjLocked();        // Tell the application to launch this receiver.        //设置component组件,为launch做准备        r.intent.setComponent(r.curComponent);        boolean started = false;        try {            if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG,                    "Delivering to component " + r.curComponent                    + ": " + r);            mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());            //重点是这个方法,调用接着传递下去            app.thread.scheduleReceiver(new Intent(r.intent), r.curReceiver,                    mService.compatibilityInfoForPackageLocked(r.curReceiver.applicationInfo),                    r.resultCode, r.resultData, r.resultExtras, r.ordered, r.userId,                    app.repProcState);            if (DEBUG_BROADCAST)  Slog.v(TAG,                    "Process cur broadcast " + r + " DELIVERED for app " + app);            started = true;        } finally {            if (!started) {                if (DEBUG_BROADCAST)  Slog.v(TAG,                        "Process cur broadcast " + r + ": NOT STARTED!");                r.receiver = null;                r.curApp = null;                app.curReceiver = null;            }        }    }

Step8:IApplicationThread.java中定义scheduleReceiver

void scheduleReceiver(Intent intent, ActivityInfo info,                       CompatibilityInfo compatInfo,                                            int resultCode, String data,                       Bundle extras, boolean sync)                       throws RemoteException;

Step9:调用到ActivityThread.java中定义scheduleReceiver

public final void scheduleReceiver(Intent intent, ActivityInfo info,                CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,                boolean sync, int sendingUser, int processState) {            updateProcessState(processState, false);            ReceiverData r = new ReceiverData(intent, resultCode, data, extras,                    sync, false, mAppThread.asBinder(), sendingUser);            r.info = info;            r.compatInfo = compatInfo;            sendMessage(H.RECEIVER, r);        }

接着看RECEIVER这个handleMessage():

public void handleMessage(Message msg) {            if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));            case RECEIVER:                    Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");                    handleReceiver((ReceiverData)msg.obj);                    maybeSnapshot();                    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);... ... }

接着来看看handleReceiver()这个方法:

private void handleReceiver(ReceiverData data) {        // If we are getting ready to gc after going to the background, well        // we are back active so skip it.        unscheduleGcIdler();        String component = data.intent.getComponent().getClassName();        LoadedApk packageInfo = getPackageInfoNoCheck(                data.info.applicationInfo, data.compatInfo);        IActivityManager mgr = ActivityManagerNative.getDefault();        BroadcastReceiver receiver;        try {            java.lang.ClassLoader cl = packageInfo.getClassLoader();            data.intent.setExtrasClassLoader(cl);            data.intent.prepareToEnterProcess();            data.setExtrasClassLoader(cl);            receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();        } catch (Exception e) {            if (DEBUG_BROADCAST) Slog.i(TAG,                    "Finishing failed broadcast to " + data.intent.getComponent());            data.sendFinished(mgr);            throw new RuntimeException(                "Unable to instantiate receiver " + component                + ": " + e.toString(), e);        }        try {            Application app = packageInfo.makeApplication(false, mInstrumentation);            if (localLOGV) Slog.v(                TAG, "Performing receive of " + data.intent                + ": app=" + app                + ", appName=" + app.getPackageName()                + ", pkg=" + packageInfo.getPackageName()                + ", comp=" + data.intent.getComponent().toShortString()                + ", dir=" + packageInfo.getAppDir());            ContextImpl context = (ContextImpl)app.getBaseContext();            sCurrentBroadcastIntent.set(data.intent);            receiver.setPendingResult(data);//这个方法是回调到调用者的onReceive()中去。            receiver.onReceive(context.getReceiverRestrictedContext(),                    data.intent);        } catch (Exception e) {            if (DEBUG_BROADCAST) Slog.i(TAG,                    "Finishing failed broadcast to " + data.intent.getComponent());            data.sendFinished(mgr);            if (!mInstrumentation.onException(receiver, e)) {                throw new RuntimeException(                    "Unable to start receiver " + component                    + ": " + e.toString(), e);            }        } finally {            sCurrentBroadcastIntent.set(null);        }        if (receiver.getPendingResult() != null) {            data.finish();        }    }

Step10:下面来看看deliverToRegisteredReceiverLocked()这个方法

private final void deliverToRegisteredReceiverLocked(BroadcastRecord r,BroadcastFilter filter, boolean ordered) {        boolean skip = false;        if (filter.requiredPermission != null) {            int perm = mService.checkComponentPermission(filter.requiredPermission,                    r.callingPid, r.callingUid, -1, true);            if (perm != PackageManager.PERMISSION_GRANTED) {                Slog.w(TAG, "Permission Denial: broadcasting "                        + r.intent.toString()                        + " from " + r.callerPackage + " (pid="                        + r.callingPid + ", uid=" + r.callingUid + ")"                        + " requires " + filter.requiredPermission                        + " due to registered receiver " + filter);                skip = true;            }        }        if (!skip && r.requiredPermission != null) {            int perm = mService.checkComponentPermission(r.requiredPermission,                    filter.receiverList.pid, filter.receiverList.uid, -1, true);            if (perm != PackageManager.PERMISSION_GRANTED) {                Slog.w(TAG, "Permission Denial: receiving "                        + r.intent.toString()                        + " to " + filter.receiverList.app                        + " (pid=" + filter.receiverList.pid                        + ", uid=" + filter.receiverList.uid + ")"                        + " requires " + r.requiredPermission                        + " due to sender " + r.callerPackage                        + " (uid " + r.callingUid + ")");                skip = true;            }        }        //校验权限        if (r.appOp != AppOpsManager.OP_NONE) {            int mode = mService.mAppOpsService.noteOperation(r.appOp,                    filter.receiverList.uid, filter.packageName);            if (mode != AppOpsManager.MODE_ALLOWED) {                if (DEBUG_BROADCAST)  Slog.v(TAG,                        "App op " + r.appOp + " not allowed for broadcast to uid "                        + filter.receiverList.uid + " pkg " + filter.packageName);                skip = true;            }        }        if (!skip) {            skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,                    r.callingPid, r.resolvedType, filter.receiverList.uid);        }        if (filter.receiverList.app == null || filter.receiverList.app.crashing) {            Slog.w(TAG, "Skipping deliver [" + mQueueName + "] " + r                    + " to " + filter.receiverList + ": process crashing");            skip = true;        }        if (!skip) {            // If this is not being sent as an ordered broadcast, then we            // don't want to touch the fields that keep track of the current            // state of ordered broadcasts.            if (ordered) {                r.receiver = filter.receiverList.receiver.asBinder();                r.curFilter = filter;                filter.receiverList.curBroadcast = r;                r.state = BroadcastRecord.CALL_IN_RECEIVE;                if (filter.receiverList.app != null) {                    // Bump hosting application to no longer be in background                    // scheduling class.  Note that we can't do that if there                    // isn't an app...  but we can only be in that case for                    // things that directly call the IActivityManager API, which                    // are already core system stuff so don't matter for this.                    r.curApp = filter.receiverList.app;                    filter.receiverList.app.curReceiver = r;                    mService.updateOomAdjLocked(r.curApp);                }            }            try {                if (DEBUG_BROADCAST_LIGHT) {                    int seq = r.intent.getIntExtra("seq", -1);                    Slog.i(TAG, "Delivering to " + filter                            + " (seq=" + seq + "): " + r);                }                //这个方法是重点看的                performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver,                    new Intent(r.intent), r.resultCode, r.resultData,                    r.resultExtras, r.ordered, r.initialSticky, r.userId);                if (ordered) {                    r.state = BroadcastRecord.CALL_DONE_RECEIVE;                }            } catch (RemoteException e) {                Slog.w(TAG, "Failure sending broadcast " + r.intent, e);                if (ordered) {                    r.receiver = null;                    r.curFilter = null;                    filter.receiverList.curBroadcast = null;                    if (filter.receiverList.app != null) {                        filter.receiverList.app.curReceiver = null;                    }                }            }        }    }
private static void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver,            Intent intent, int resultCode, String data, Bundle extras,            boolean ordered, boolean sticky, int sendingUser) throws RemoteException {        // Send the intent to the receiver asynchronously using one-way binder calls.        if (app != null) {            if (app.thread != null) {                // If we have an app thread, do the call through that so it is                // correctly ordered with other one-way calls.                //这个方法是重点                app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode,                        data, extras, ordered, sticky, sendingUser, app.repProcState);            } 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);        }    }

Step11:下面来看看scheduleRegisteredReceiver()这个方法

在ActivityThread.java这个类中

// This function exists to make sure all receiver dispatching is        // correctly ordered, since these are one-way calls and the binder driver        // applies transaction ordering per object for such calls.        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指向了一个InnerReceiver对象,  //每一个InnerReceiver对象在内部都封装了一个广播接收者。            receiver.performReceive(intent, resultCode, dataStr, extras, ordered,                    sticky, sendingUser);        }

Step12:下面来看看receiver.performReceive()这个方法

这个方法定义在LoadedApk.java中,路径
framework/base/core/java/android/app/LoadedApk.java

static final class ReceiverDispatcher {    final static class InnerReceiver extends IIntentReceiver.Stub {            final WeakReference<LoadedApk.ReceiverDispatcher> mDispatcher;            final LoadedApk.ReceiverDispatcher mStrongRef;            InnerReceiver(LoadedApk.ReceiverDispatcher rd, boolean strong) {                mDispatcher = new WeakReference<LoadedApk.ReceiverDispatcher>(rd);                mStrongRef = strong ? rd : null;            }            public void performReceive(Intent intent, int resultCode, String data,                    Bundle extras, boolean ordered, boolean sticky, int sendingUser) {                LoadedApk.ReceiverDispatcher 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,                            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 = ActivityManagerNative.getDefault();                    try {                        if (extras != null) {                            extras.setAllowFds(false);                        }                        mgr.finishReceiver(this, resultCode, data, extras, false);                    } catch (RemoteException e) {                        Slog.w(ActivityThread.TAG, "Couldn't finish broadcast to unregistered receiver");                    }                }            }        }

接着看ReceiverDispatcher 内部类的performReceive()这个方法:

public void performReceive(Intent intent, int resultCode, String data,                Bundle extras, boolean ordered, boolean sticky, int sendingUser) {            if (ActivityThread.DEBUG_BROADCAST) {                int seq = intent.getIntExtra("seq", -1);                Slog.i(ActivityThread.TAG, "Enqueueing broadcast " + intent.getAction() + " seq=" + seq                        + " to " + mReceiver);            }            Args args = new Args(intent, resultCode, data, extras, ordered,                    sticky, sendingUser);        //if语句会调用到args中的run()方法中去的:            if (!mActivityThread.post(args)) {                if (mRegistered && ordered) {                    IActivityManager mgr = ActivityManagerNative.getDefault();    if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG,                            "Finishing sync broadcast to " + mReceiver);                    args.sendFinished(mgr);                }            }        }

Step13:最后来看看Args 这个run()方法:

final class Args extends BroadcastReceiver.PendingResult implements Runnable {            private Intent mCurIntent;            private final boolean mOrdered;            public Args(Intent intent, int resultCode, String resultData, Bundle resultExtras,                    boolean ordered, boolean sticky, int sendingUser) {                super(resultCode, resultData, resultExtras,                        mRegistered ? TYPE_REGISTERED : TYPE_UNREGISTERED,                        ordered, sticky, mIIntentReceiver.asBinder(), sendingUser);                mCurIntent = intent;                mOrdered = ordered;            }            public void run() {                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 = ActivityManagerNative.getDefault();                final Intent intent = mCurIntent;                mCurIntent = null;                if (receiver == null || mForgotten) {                    if (mRegistered && ordered) {                        if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG,                                "Finishing null broadcast to " + mReceiver);                        sendFinished(mgr);                    }                    return;                }                Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveReg");                try {                    ClassLoader cl =  mReceiver.getClass().getClassLoader();                    intent.setExtrasClassLoader(cl);                    setExtrasClassLoader(cl);                    receiver.setPendingResult(this);                    //这个方法回调到注册者的onReceive()中去了。                    receiver.onReceive(mContext, intent);                } 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);            }        }
1 0
原创粉丝点击