设为首页 - 加入收藏 焦点技术网
热搜:java
当前位置:首页 >

Android4.2 broadcast 分析

2013-06-20 15:45:00.0 Android  
导读:本文Siobhan给大家介绍 Android4.2 broadcast 分析。     最近正好有一个关于Broadcast的ANR的bug,所以仔细的去看看了关于Broadcast的代码。以前只知。。。

     最近正好有一个关于Broadcast的ANR的bug,所以仔细的去看看了关于Broadcast的代码。以前只知道去使用它,也就是纯粹的send和onReceive。

     现在趁热打铁把之前看的东西记录一下。

     一、 关于BroadcastReceiver的两种注册方式。

     二、 发送广播后之后的或处理流程。


一、接下来就先看一下BroadcastReceiver的注册:

1. 第一种方法通过在AndroidManitest.xml中添加 tag, 使用方法http://developer.android.com/guide/topics/manifest/receiver-element.html

1.1  我们知道在安装一个apk时,PMS会去解析这个apk里面AndroidManitest的信息,首先应该会调用PMS.installNewPackageLI() --->   PMS.scanPackageLI() ---> pp.parsePackage()......这时候就会通过PackageParser.parsePackage()去解析AndroidManitest中包含在application里面的部分,而我们的receiver就是写在这个里面。

        在PMS.scanPackageLI函数中会去遍历带有解析结果的Package对象,如果发现里面有Receiver就放到mReceivers数组中,以后查询的时候就是在这个数组里面找。

    private Package parsePackage(        Resources res, XmlResourceParser parser, int flags, String[] outError)        throws XmlPullParserException, IOException {        ... ...        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT                && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {                      if (tagName.equals("application")) {                if (foundApp) {                ... ...                foundApp = true;                if (!parseApplication(pkg, res, parser, attrs, flags, outError)) {                    return null;                }            } else if (tagName.equals("permission")) {               ... ...            } else if (tagName.equals("permission-tree")) {               ... ...            } else if (tagName.equals("uses-permission")) {               ... ...            }        return pkg;    }   

1.2  之后PackageParser就用去调用parseApplication去继续解析tag application里面的注册的东西。

       在这里边如果写有receiver,那么就通过parseActivity把它解析出来然后放在owner.receivers数组中。owner就是一个Package对象。

       这个数组会随着Package传回给PMS。

    private boolean parseApplication(Package owner, Resources res,            XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)        throws XmlPullParserException, IOException {                 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT                && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {            if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {                continue;            }            String tagName = parser.getName();            if (tagName.equals("activity")) {                ... ...                owner.activities.add(a);            } else if (tagName.equals("receiver")) {                Activity a = parseActivity(owner, res, parser, attrs, flags, outError, true, false);                owner.receivers.add(a);            } else if (tagName.equals("service")) {                ... ...                owner.services.add(s);


2. 第二种方法通过Context.registerReceiver()

2.1  ContextImpl.registerReceiver

        ContextImpl的registerReceiver就纯粹调用内部函数registerReceiverInternal();

    public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {        return registerReceiver(receiver, filter, null, null);    }


2.2  ContextImpl.registerReceiverInternal() 

     1. 如果mPackageInfo 也就是LoadededApk 之前已经初始化并且context != null,那么就会通过mPackageInfo.getRecieverDispatcher去获取一个“LoadedApk.ReceiverDispatcher” 。再LoadedApk会根据不同的Context保存一个HashMap, 并且会把这个HashMap保存在mReceivers数组。

         这里为什么要出现BroadcastReceiver和LoadedApk.ReceiverDispatcher对应的Map出现,因为ContextImpl需要把BroadcastReceiver传给AMS,然后AMS需要直接调用这个BroadcastReceiver而不是通过AMS自己的Binder。然而BroadcastReceiver并没有实现Binder机制,所以这里就引出了LoadedApk.ReceiverDispatcher, 这个类主要就是干这个活,AMS可以通过它回调然后再去调用BroadcastReceiver的方法,等同于是AMS和BroadcastReceiver的一个桥梁。

      2. 通过ActivityManagerNative.getDefault().registerReceiver去调用AMS的registerReceiver并把ReceiverDispatcher传递过去。

    private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,            IntentFilter filter, String broadcastPermission,            Handler scheduler, Context context) {        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);            } else {                if (scheduler == null) {                    scheduler = mMainThread.getHandler();                }                rd = new LoadedApk.ReceiverDispatcher(                        receiver, context, scheduler, null, true).getIIntentReceiver();            }        }        try {            return ActivityManagerNative.getDefault().registerReceiver(                    mMainThread.getApplicationThread(), mBasePackageName,                    rd, filter, broadcastPermission, userId);        } catch (RemoteException e) {            return null;        }    }

2.2.2 AMS.registerReceiver()

      1. 在mRegisteredReceivers中先根据传过来ReceiverDispatcher查看是否之前已经注册过了,如果没有,就new一个新的ReceiverList;然后把对应的IIntentReceiver(也就是ReceiverDispatcher)和ReceiverList保存在  mRegisteredReceivers Map数组中。

      2. 根据新传过来的IntentFilter和刚才生成的ReceiverList组成一个BroadcastFilter, 并且将这个BroadcastFilter 加到rl 和 mReceiverResolver.addFilter中。

到这里Receiver已经注册到AMS中。

    public Intent registerReceiver(IApplicationThread caller, String callerPackage,            IIntentReceiver receiver, IntentFilter filter, String permission, int userId) {        enforceNotIsolatedCaller("registerReceiver");        int callingUid;        int callingPid;        synchronized(this) {            ProcessRecord callerApp = null;            ... ...            ReceiverList rl                = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder());            if (rl == null) {                rl = new ReceiverList(this, callerApp, callingPid, callingUid,                        userId, receiver);                if (rl.app != null) {                    rl.app.receivers.add(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,                    permission, callingUid, userId);            rl.add(bf);            if (!bf.debugCheck()) {                Slog.w(TAG, "==> For Dynamic broadast");            }            mReceiverResolver.addFilter(bf);            return sticky;        }    }



二、 发送广播后之后的处理流程

1. ContextImpl.sendBroadcast(Intent intent)

     在ContextImpl.sendBroadcas通过ActivityManagerNative binder去调用AMS的broadcastIntent的方法。

    public void sendBroadcast(Intent intent) {        warnIfCallingFromSystemProcess();        String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());        try {            intent.setAllowFds(false);            ActivityManagerNative.getDefault().broadcastIntent(                mMainThread.getApplicationThread(), intent, resolvedType, null,                Activity.RESULT_OK, null, null, null, false, false,                getUserId());        } catch (RemoteException e) {        }    }

2. AMS.broadcastIntent

    在broadcastIntent中会获得调用的进程信息,比如ProcessRecord,callingPid,callingUid ,然后调用broadcastIntentLocked 去做真正的发送broadcast。

    这里面会传来一个参数serialized = false; 这个参数对应于后面的ordered参数,表示当前的broadcast是不是要序列化的发送。

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

2.1 AMS.broadcastIntentLocked()

     1. 这个函数前面主要是做一些权限等相关的检查,如果不满足就直接return了。

     2. 之后去处理一些系统级别的broadcast,比如package安装,删除和TIMEZONE_CHANGED等等。

     3. 最后去查找真正需要接收这个broadcast的receivers

         1) 如果intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY  == 0,也就是这个Intent并不是只能传递给通过Register的方式的Reciever,那么就通过collectReceiverComponents去通过PMS查询在Manifest中注册Receivers,然后把符合条件的Receivers放到receivers数组中。

         2)如果intent.getComponent() == null, 也就是没有指定接收的component,那么就通过 mReceiverResolver.queryIntent去查询通过Register方式注册的Recievers,然后把符合条件的Receivers返回给registeredReceivers数组列表中。

         PS: receivers 就是一个List

                   registeredReceivers是 List, 也就是之前我们保存在mReceiverResolver map中的BroadcastFilter。而BroadcastFilter中有一个ReceiverList。

       4.  查询完Receiver之后,会通过 replacePending = (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0;   这个参数去判断如果之前有一样的Broadcast是否要用新的这个覆盖掉之前的那个。

        5.  最后把这个Broadcast放到BroadcastQueue中,如果ordered是false并且registeredReceivers不为空,那么就先处理registeredReceivers,也就是动态注册的Recievers。

             在AMS中两个BroadcastQueue,一个是前台的mFgBroadcastQueue,另一个是mBgBroadcastQueue。AMS通过函数broadcastQueueForIntent去查询Intent是否有指定FLAG_RECEIVER_FOREGROUND来判断是前台的还是后台的。

             之后将对应的Intent,BroadcastQueue,registeredReceivers,ordered封装到一个BroadcastRecord中,用来记录这个Broadcast。如果replacePending是true,通过queue.replaceParallelBroadcastLocked(r)查询BroadcastQueue中是否有一样的BroadcastRecord如果有的话就repace掉。

             如果没有需要replace的BroadcastRecord,就需要把这个新的BroadcastRecord放到queue中,通过函数queue.enqueueParallelBroadcastLocked(r);       

          6. 处理完registeredReceivers之后,会根据一些特殊的判断把registeredReceivers合并到receivers之中。接着去处理在Manifest中注册的可以排序处理的receivers. 同样会封装成一个BroadcastRecord,唯一不同是调用queue.enqueueOrderedBroadcastLocked(r); 去发送。

          PS:这里要说明一下,为什么动态注册的Receiver和写在Manifest中的Receiver会调用不同的接口呢,因为我们在发送BroadCast的时候可以通过sendBroadCast来发送普通的或者通过sendOrderedBroadCast来发送排序的。所以在Framework中默认把动态注册的Receiver认为是非排序的而Manifest中的Receiver是可以排序的,也就是动态注册的Receiver不能把处理结果传递给下一个Reciever而Manifest中的Receiver可以传递而已还可以拦截。

         具体可以看一下 http://developer.android.com/reference/android/content/BroadcastReceiver.html

          7. AMS通过queue.scheduleBroadcastsLocked();去通知BroadcastQueue处理新加进去的Broadcast。

    private final int broadcastIntentLocked(ProcessRecord callerApp,            String callerPackage, Intent intent, String resolvedType,            IIntentReceiver resultTo, int resultCode, String resultData,            Bundle map, String requiredPermission,            boolean ordered, boolean sticky, int callingPid, int callingUid,            int userId) {        intent = new Intent(intent);        ... ...        userId = handleIncomingUser(callingPid, callingUid, userId,                true, false, "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;            }        }        ... ...        // 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())                || 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);                        }                    }                } 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, userId);                            }                            sendPackageBroadcastLocked(                                    IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list, userId);                        }                    } else {                        Uri data = intent.getData();                        String ssp;                        if (data != null && (ssp=data.getSchemeSpecificPart()) != null) {                            if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) {                                forceStopPackageLocked(ssp,                                        intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true,                                        false, userId);                            }                            if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {                                sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED,                                        new String[] {ssp}, userId);                            }                        }                    }                }            } 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));            }        }        /*         * 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 (intent.ACTION_CLEAR_DNS_CACHE.equals(intent.getAction())) {            mHandler.sendEmptyMessage(CLEAR_DNS_CACHE);        }        if (Proxy.PROXY_CHANGE_ACTION.equals(intent.getAction())) {            ProxyProperties proxy = intent.getParcelableExtra("proxy");            mHandler.sendMessage(mHandler.obtainMessage(UPDATE_HTTP_PROXY, proxy));        }        ... ...        // Figure out who all will receive this broadcast.        List receivers = null;        List registeredReceivers = null;        // Need to resolve the intent to interested receivers...        if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY)                 == 0) {            receivers = collectReceiverComponents(intent, resolvedType, users);        }        if (intent.getComponent() == null) {            registeredReceivers = mReceiverResolver.queryIntent(intent,                    resolvedType, false, userId);        }        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;        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, requiredPermission,                    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) {                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.            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= 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;                }            }        }        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, requiredPermission,                    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) {                queue.enqueueOrderedBroadcastLocked(r);                queue.scheduleBroadcastsLocked();            }        }        return ActivityManager.BROADCAST_SUCCESS;    }


3. 关于Broadcast的处理都是在BroadcastQueue中

    跟着enqueueParallelBroadcastLocked 和enqueueOrderedBroadcastLocked往下分析。

3.1 这两个函数都只是把一个BroadcastRecord放到对应的非排序或排序的数组中。

    public void enqueueParallelBroadcastLocked(BroadcastRecord r) {        mParallelBroadcasts.add(r);    }    public void enqueueOrderedBroadcastLocked(BroadcastRecord r) {        mOrderedBroadcasts.add(r);    }

3.2 BroadcastQueue.scheduleBroadcastsLocked()

     如果之前已经有调用scheduleBroadcastsLocked那么mBroadcastsScheduled就为true,之后就直接返回了。

     如果mBroadcastsScheduled==fasle, 就通过mHandler发送一个BROADCAST_INTENT_MSG的Message。

    public void scheduleBroadcastsLocked() {        if (DEBUG_BROADCAST) Slog.v(TAG, "Schedule broadcasts ["                + mQueueName + "]: current="                + mBroadcastsScheduled);        if (mBroadcastsScheduled) {            return;        }        mHandler.sendMessage(mHandler.obtainMessage(BROADCAST_INTENT_MSG, this));        mBroadcastsScheduled = true;    }

3.3 BroadcastQueue.mHander.handleMessage "BROADCAST_INTENT_MSG"

      mHandler接收到BROADCAST_INTENT_MSG message后,就调用processNextBroadcast去处理。

    final Handler mHandler = new Handler() {        public void handleMessage(Message msg) {            switch (msg.what) {                case BROADCAST_INTENT_MSG: {                    if (DEBUG_BROADCAST) Slog.v(                            TAG, "Received BROADCAST_INTENT_MSG");                    processNextBroadcast(true);                } break;                case BROADCAST_TIMEOUT_MSG: {                    synchronized (mService) {                        broadcastTimeoutLocked(true);                    }                } break;            }        }    };

3.4 BroadcastQueue.processNextBroadcast

     1. 首先处理non-serialized 的broadcasts,也就是保存在mParallelBroadcasts中的broadcasts。

          1)deliverToRegisteredReceiverLocked去做调用Receivers。

          2)当处理完成后,把这个broadcast放到BroadcastToHistory中。//addBroadcastToHistoryLocked

     2. 处理完mParallelBroadcasts里面所有的中BroadcastRecord之后,接着去处理mOrderedBroadcasts数组中的serialized的BroadcastRecords

         1)先检查下mPendingBroadcast是否为空,如果不为空说明当前还有serialized的Receiver正在处理,当前的Broadcast没有处理完,直接return

         2)接着从mOrderedBroadcasts取出一个BroadcastRecord,查看当前BroadcastRecord中的Recievers是否为空,如果不为空,说明还有receiver要处理。通过Object nextReceiver = r.receivers.get(recIdx);取出下一个Receiver。

               如果这个Receiver所在的Process已经起来了就直接调用processCurBroadcastLocked(r, app);来处理

               如果这个Receiver所在的Process没有起来,就调用mService.startProcessLocked然后等进程起来后去处理当前的Receiver。具体的流程跟下面都差不多。

         PS:每一个serialized的Receiver完成onReceive后都会调finish();去通知BroadcastQueue里的finishReceiverLocked传达当前的receiver已经处理结束了。。如果处理时间太长就会引起ANR。

    final void processNextBroadcast(boolean fromMsg) {        synchronized(mService) {            BroadcastRecord r;            ... ...            // First, deliver any non-serialized broadcasts right away.            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 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);                        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) isnt kept in the mBroadcastHistory.                            r.resultTo = null;                        } catch (RemoteException e) {                            Slog.w(TAG, "Failure ["                                    + mQueueName + "] sending broadcast result of "                                    + r.intent, e);                        }                    }                    if (DEBUG_BROADCAST) Slog.v(TAG, "Cancelling 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);            }            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);            ... ...            // Hard case: need to instantiate the receiver, possibly            // starting its application process to host it.            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 (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;                }            }            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.                if (DEBUG_BROADCAST)  Slog.v(TAG,                        "Skipping deliver ordered ["                        + mQueueName + "] " + r + " to " + r.curApp                        + ": process crashing");                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;            if (r.callingUid != Process.SYSTEM_UID && isSingleton) {                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);            }            // 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);            if (app != null && app.thread != null) {                try {                    app.addPackage(info.activityInfo.packageName);                    processCurBroadcastLocked(r, app);                    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.            ... ...            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))                            == 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, true);                scheduleBroadcastsLocked();                r.state = BroadcastRecord.IDLE;                return;            }            mPendingBroadcast = r;            mPendingBroadcastRecvIndex = recIdx;        }    }

3.4.1 BroadcastQueue.deliverToRegisteredReceiverLocked()

        1. 先检查在BroadcastRecord 里面的Receivers的Permission,如果不满足就skip

        2. 调用performReceiveLocked

    private final void deliverToRegisteredReceiverLocked(BroadcastRecord r,            BroadcastFilter filter, boolean ordered) {        long startTime = System.currentTimeMillis();        ... ...        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.            ... ...            try {                ... ...                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;                }            }         }        ... ...    }

3.4.1.2 BroadcastQueue.performReceiveLocked()

       1.如果发送这个receiver所在的进程存在,那么就通过app.thread.scheduleRegisteredReceiver()回调到这个进程当中。

       2. 要不然就直接通过receiver的Binder去调用Receiver.performRecieve函数。

    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 && 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);        } else {            receiver.performReceive(intent, resultCode, data, extras, ordered,                    sticky, sendingUser);        }    }

3.4.1.2.1 ActivityThread.scheduleRegisteredReceiver()

        1.  通过receiver的binder调用,走到ReceiverDispatcher.InnerReceiver.performReceive()函数中。

        2.  在ReceiverDispatcher.InnerReceiver.performReceive()中会post一个runnable到主消息队列中去调用对应receiver的onReceive()的函数。

        3. 如果receiver.getPendingResult() != null 就调用finish();去通知AMS  //如果是非序列的reciever,那么getPendingResult应该返回的是null

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

        final class Args extends BroadcastReceiver.PendingResult implements Runnable {            private Intent mCurIntent;            private final boolean mOrdered;            ... ...                        public void run() {                final BroadcastReceiver receiver = mReceiver;                final boolean ordered = mOrdered;                ... ...                try {                    ClassLoader cl =  mReceiver.getClass().getClassLoader();                    intent.setExtrasClassLoader(cl);                    setExtrasClassLoader(cl);                    receiver.setPendingResult(this);                    receiver.onReceive(mContext, intent);                } catch (Exception e) {                    ... ...                }                                if (receiver.getPendingResult() != null) {                    finish();                }                Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);            }        }







(编辑: Siobhan)

网友评论
相关文章