一、广播
1、定义
1)、类似观察者模式
2、场景
1)、同一个app内多个进程的不同组件之间的消息通信
2)、不同的app之间的组件之间消息通信
3、种类
1)、普通广播:Context.sendBroadcast()
2)、有序广播:Context.sendOrderedBroadCast()
3)、本地广播:自在App内传播
二、实现广播-receiver
1、静态注册:
a、直接把广播接收者写在manifast中;
b、注册完成就一直运行;
c、依赖的activity被销毁了,仍然接收广播;
d、甚至app 进程被杀死了后仍能收到广播;
2、动态注册:
a、在代码中调用registerServer();
b、跟随acticity的生命周期,activity被销毁了,广播接收者也就失效了;
c、注意在destory()方法中unRegisterServier()来防止内存泄漏;
三、广播实现机制
AMS : 贯穿android系统组件的一个核心服务,负责四大组件的启动,切换和调度,以及应用程序的管理和调度工作;
四、LocalBroadcastManager详解
1、三个集合类
1、
private final HashMap<BroadcastReceiver, ArrayList<IntentFilter>> mReceivers
= new HashMap<BroadcastReceiver, ArrayList<IntentFilter>>();
key是BroadcastReceiver,value是每个BroadcastReceiver对应可以接收几个action的广播;
2、
private final HashMap<String, ArrayList<ReceiverRecord>> mActions
= new HashMap<String, ArrayList<ReceiverRecord>>();
key是action,value 是action 对应的ReceiverRecord的集合;
private static class ReceiverRecord {
final IntentFilter filter;
final BroadcastReceiver receiver;
}
3、
private final ArrayList<BroadcastRecord> mPendingBroadcasts
= new ArrayList<BroadcastRecord>();
mPendingBroadcasts是存储 和发送的广播action匹配的 ReceiverRecord集合;
private static class BroadcastRecord {
final Intent intent;
final ArrayList<ReceiverRecord> receivers;
}
2、