Android 四大组件之 Broadcast Record

一. 引言

广播在Android系统使用频率比较高,广播的使用场景往往是在满足某种条件下发出一个事件(broadcast),多处(Receiver)可以监听该事件通知并做出相应的改变。比如亮/灭屏,网络状态切换等事件发送时都会发出相应的广播。

    LoadedApk
        ReceiverDispatcher
            InnerReceiver extends IIntentReceiver.Stub
            Args extends BroadcastReceiver.PendingResult implements Runnable

        ServiceDispatcher
            InnerConnection extends IServiceConnection.Stub
            RunConnection implements Runnable

关系:

  • InnerReceiver与ReceiverDispatcher在数量上一一对应;
  • InnerConnection与ServiceDispatcher在数量上也一一对应;

二. Broadcast数据结构

点击查看大图

2.1 BroadcastRecord

  1. callerApp:广播发送者的进程;
  2. ordered:是否有序广播;
  3. sticky:是否粘性广播;
  4. receivers:广播接收器,包括动态注册(BroadcastFilter)和静态注册(ResolveInfo);
  5. receiver:数据类型为IBinder,保存的广播所在进程的ApplicationThread对象的代理端
  6. 时间点:
  • enqueueClockTime: 广播入队列时间点;
  • dispatchTime:广播分发时间点;
  • receiverTime:当前receiver开始处理时间点;
  • finishTime:广播处理完成时间点;

2.2 BroadcastFilter

只有registerReceiver()过程才会创建BroadcastFilter,也就是该对象用于动态注册的广播Receiver;
BroadcastFilter继承于IntentFilter.

  • packageName:发起注册广播接收器所对应的包名;
  • owningUid:广播接收器所对应的uid;

2.3 ReceiverList

同样地, 只有registerReceiver()过程才会创建ReceiverList;

2.4 AMS

点击查看大图

top Created with Sketch.