【Android源码】BroadcastReceiver的工作过程

BroadcastReceiver的使用

通常情况下,我们使用广播的方式,首先定义广播接收者,继承BroadcastReceiver并重写onReceive方法:

public class MyReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent){
        Log.d("TAG", "on receive action = " + intent.getAction());
    }
}

定义好广播之后,就可以注册广播接收者了。
有两种方式:静态注册和动态注册。

静态注册:

<receiver android:name=".MyReceiver">
    <intent-filter>
        <action android:name="com.fastaoe.receiver.LAUNCH"/>
    </intent-filter>
</receiver>

动态注册:

IntentFilter filter = new IntentFilter();
filter.addAction("com.fastaoe.receiver.LAUNCH");
registerReceiver(new MyReceiver(), filter);

当注册完成之后就可以通过send来发送广播了:

Intent intent = new Intent();
intent.setAction("com.fastaoe.register.LAUNCH");
sendBroadcast(intent);

BroadcastReceiver的注册过程

静态注册的过程其实就是PackageManagerService解析的过程,其实四大组件都是有PMS来解析并注册的可以参考【Android源码】PackageManagerService 浅析

我们现在只分析动态注册的过程:

同样的动态注册和Activity和Service一样都是在ContextWrapper中,而其实mBase的具体实现类是ContextImpl对象:

// ContextWrapper.java
@Override
public Intent registerReceiver(
   BroadcastReceiver receiver, IntentFilter filter) {
   return mBase.registerReceiver(receiver, filter);
}

// ContextImpl.java
@Override
public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
   return registerReceiver(receiver, filter, null, null);
}

@Override
public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
       String broadcastPermission, Handler scheduler) {
   return registerReceiverInternal(receiver, getUserId(),
           filter, broadcastPermission, scheduler, getOuterContext());
}

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 {
       final Intent intent = ActivityManagerNative.getDefault().registerReceiver(
               mMainThread.getApplicationThread(), mBasePackageName,
               rd, filter, broadcastPermission, userId);
       if (intent != null) {
           intent.setExtrasClassLoader(getClassLoader());
           intent.prepareToEnterProcess();
       }
       return intent;
   } catch (RemoteException e) {
       throw e.rethrowFromSystemServer();
   }
}

上述代码主要做了这样几件事:

  1. mPackageInfo中获取IIntentReceiver对象。

    因为广播是可以跨进程通信的,所以不能直接使用BroadcastReceiver,而是使用InnerReceiver extends IIntentReceiver.Stub的类型,也就是Binder接口,这个其实和Service的绑定流程类似。

  2. 通过AMS注册广播。

// ActivityManagerService.java
public Intent registerReceiver(IApplicationThread caller, String callerPackage,
            IIntentReceiver receiver, IntentFilter filter, String permission, int userId) {
    mRegisteredReceivers.put(receiver.asBinder(), rl);
    
    BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage,
                    permission, callingUid, userId);
    rl.add(bf);     
}  

通过registerReceiver方法将IIntentReceiver和IntentFilter保存起来,这个时候广播就被注册好了。

BroadcastReceiver的发送和接收过程

同样的sendBroadcast也是由ContextImpl来实现的:

@Override
public void sendBroadcast(Intent intent) {
   warnIfCallingFromSystemProcess();
   String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
   try {
       intent.prepareToLeaveProcess(this);
       ActivityManagerNative.getDefault().broadcastIntent(
               mMainThread.getApplicationThread(), intent, resolvedType, null,
               Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, false,
               getUserId());
   } catch (RemoteException e) {
       throw e.rethrowFromSystemServer();
   }
}

上述代码什么都没做,只是使用AMS调用broadcastIntent

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

       final ProcessRecord callerApp = getRecordForAppLocked(caller);
       final int callingPid = Binder.getCallingPid();
       final int callingUid = Binder.getCallingUid();
       final long origId = Binder.clearCallingIdentity();
       int res = broadcastIntentLocked(callerApp,
               callerApp != null ? callerApp.info.packageName : null,
               intent, resolvedType, resultTo, resultCode, resultData, resultExtras,
               requiredPermissions, appOp, bOptions, serialized, sticky,
               callingPid, callingUid, userId);
       Binder.restoreCallingIdentity(origId);
       return res;
   }
}

从上面的代码可以看到是调用broadcastIntentLocked方法:

intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES);

在开始的时候添加了一个特殊的标记,这个标记表明默认情况下,广播不会发送给已经停止的应用。

if ((receivers != null && receivers.size() > 0)
      || resultTo != null) {
  BroadcastQueue queue = broadcastQueueForIntent(intent);
  BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp,
          callerPackage, callingPid, callingUid, resolvedType,
          requiredPermissions, appOp, brOptions, receivers, resultTo, resultCode,
          resultData, resultExtras, ordered, sticky, false, userId);

  if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Enqueueing ordered broadcast " + r
          + ": prev had " + queue.mOrderedBroadcasts.size());
  if (DEBUG_BROADCAST) Slog.i(TAG_BROADCAST,
          "Enqueueing broadcast " + r.intent.getAction());

  boolean replaced = replacePending && queue.replaceOrderedBroadcastLocked(r);
  if (!replaced) {
      queue.enqueueOrderedBroadcastLocked(r);
      queue.scheduleBroadcastsLocked();
  }
}

之后broadcastIntentLocked内部,会根据intent-filter来过滤匹配所有的广播接收者,最终满足所有条件的广播接收者会被添加到BroadcastQueue中,之后BroadcastQueue会通过scheduleBroadcastsLocked将广播发送给这些符合条件的广播接收者。

public void scheduleBroadcastsLocked() {
   if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Schedule broadcasts ["
           + mQueueName + "]: current="
           + mBroadcastsScheduled);

   if (mBroadcastsScheduled) {
       return;
   }
   mHandler.sendMessage(mHandler.obtainMessage(BROADCAST_INTENT_MSG, this));
   mBroadcastsScheduled = true;
}

scheduleBroadcastsLocked,系统并没有直接发送广播,而是通过handler来发送消息给BroadcastHandler,而BroadcastHandler在接收到BROADCAST_INTENT_MSG之后调用了processNextBroadcast

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_BROADCAST, "Processing parallel broadcast ["
             + mQueueName + "] " + r);
     for (int i=0; i<N; i++) {
         Object target = r.receivers.get(i);
         if (DEBUG_BROADCAST)  Slog.v(TAG_BROADCAST,
                 "Delivering non-ordered on [" + mQueueName + "] to registered "
                 + target + ": " + r);
         deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false, i);
     }
     addBroadcastToHistoryLocked(r);
     if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Done with parallel broadcast ["
             + mQueueName + "] " + r);
}

通过循环遍历mParallelBroadcasts并将广播发送给接收者,就是通过deliverToRegisteredReceiverLocked来完成的:

performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver,
                        new Intent(r.intent), r.resultCode, r.resultData,
                        r.resultExtras, r.ordered, r.initialSticky, r.userId);
                        
void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver,
            Intent intent, int resultCode, String data, Bundle extras,
            boolean ordered, boolean sticky, int sendingUser) throws RemoteException {
    app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode,
                            data, extras, ordered, sticky, sendingUser, app.repProcState);           
}

最终调用了ApplicationThread的scheduleRegisteredReceiver来完成广播的接收:

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

receiver其实就是之前我们所讲的InnerReceiver

@Override
public void performReceive(Intent intent, int resultCode, String data,
    Bundle extras, boolean ordered, boolean sticky, int sendingUser) {
    rd.performReceive(intent, resultCode, data, extras,
                            ordered, sticky, sendingUser);   
}

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

这里有一段关键代码mActivityThread.post(args),其中args是Args的实例,而Args实现了Runnable接口,mActivityThread是ActivityThread中的mH的Handler对象,在Args的run方法中:

ClassLoader cl =  mReceiver.getClass().getClassLoader();
intent.setExtrasClassLoader(cl);
intent.prepareToEnterProcess();
setExtrasClassLoader(cl);
receiver.setPendingResult(this);
receiver.onReceive(mContext, intent);

BroadcastReceiver的onReceive就被执行了,这个使用app也就接收到了广播。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,265评论 6 490
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,078评论 2 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 156,852评论 0 347
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,408评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,445评论 5 384
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,772评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,921评论 3 406
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,688评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,130评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,467评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,617评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,276评论 4 329
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,882评论 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,740评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,967评论 1 265
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,315评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,486评论 2 348

推荐阅读更多精彩内容