Android app启动流程:广播的发送processNextBroadcast(8)

  • A+
所属分类:AMS
    final void processNextBroadcast(boolean fromMsg) {
        synchronized(mService) {
            BroadcastRecord r;
            mService.updateCpuStats();
            if (fromMsg) {
                mBroadcastsScheduled = false;
            }

            while (mParallelBroadcasts.size() > 0) {
                r = mParallelBroadcasts.remove(0);
                r.dispatchTime = SystemClock.uptimeMillis();
                r.dispatchClockTime = System.currentTimeMillis();
                final int N = r.receivers.size();

if (app != null && app.thread != null) {
                try {
                    app.addPackage(info.activityInfo.packageName,
                            info.activityInfo.applicationInfo.versionCode, mService.mProcessStats);
                    processCurBroadcastLocked(r, app);
                    return;
                } catch (RemoteException e) {

                } catch (RuntimeException e) {

                    return;
                }

            if ((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) {
                finishReceiverLocked(r, r.resultCode, r.resultData,r.resultExtras, r.resultAbort, false);
                scheduleBroadcastsLocked();//重新调度  然后又会回到  deliverToRegisteredReceiverLocked
                r.state = BroadcastRecord.IDLE;
                return;
            }

            mPendingBroadcast = r;
            mPendingBroadcastRecvIndex = recIdx;
        }

 

processNextBroadcast 继续做广播发送的处理,如果目标在processCurBroadcastLocked

如果目标不存在 就会先建立process,然后重新调度scheduleBroadcastsLocked

private final void processCurBroadcastLocked(BroadcastRecord r,
            ProcessRecord app) throws 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();
        r.intent.setComponent(r.curComponent);

        boolean started = false;
        try {

            mService.notifyPackageUse(r.intent.getComponent().getPackageName(),
                                      PackageManager.NOTIFY_PACKAGE_USE_BROADCAST_RECEIVER);


            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);

            started = true;
        } finally {

        }
    }

 

processCurBroadcastLocked 里面就去调用scheduleReceiver 表现在应用层就是OnReceiver接收到广播。

 

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: