1.广播漫谈
按照日常生活的习惯,我们也能很容易的能够想到,广播要有人发送,也要有人接收,程序也是这样的,要有接收者也要有发送者。
2.接收广播
(1)动态注册广播
1.
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("要接收的广播(系统的和manifest的<intent-filter>中android:name)“);
2.
创建一个XXXReceiver继承自BroadcastReceiver
在onReceiver()中重写
3.
registerReceiver(xxxReceiver,intentFilter);
4.在onDestroy中unregisterReceiver(xxxReceiver);
(2)静态注册广播
a.new一个xxxreceiver继承自BroadcastReceiver,重写onReceiver
b.manifest中会有一个<receiver> </receiver>,在<intent-filter></intent-filter>
中声明action就可以接受来自系统的广播了
3. 发送广播
(1)标准广播
Intent intent = new Intent("拥有android:name的所有接收器能收到");
sendBroadcast(intent);
(2)有序广播
4.本地广播
a.发送广播
Intent intent = new Intent("要发送的action")
localBroadcastManager.sendBroadcast()
b.IntentFilter intentFilter = new IntentFilter("接收什么类型的广播")
c.创建一个broadcastReceiver
d.localBroadcastManager.register(localReceiver,intentFilter);
e.localBroadcastManager.unregister(localReceiver);