一 广播知识总结
1.知识字典
本地 LocalBroadcastManager.
无序 sendBroadcast
有序 sendOrdereadBroadcast()
注册 registerReceiver
静态 +<action android:name
处理 BroadcastReceiver+onReceiver()
2.知识结构关系图
二 简单广播关系模拟
1 目的
关于android 广播的学习,注册、发送、全局广播、局部广播、自定义广播、系统广播。
学起来有点绕,为了缕清思路和规律,我今天用java做了以下简单的模拟。
2 具体成员和动作设计
定义广播:就是定义一个字符串
有一个广播中心:收到广播后,调用注册的回调函数
注册:如果您想收到广播后做相应的处理,那么您就把您要处理的函数注册给广播中心
发广播:向广播中心发送广播(一个定义好的字符串)
广播响应:广播中心收到广播后,调用该广播下定义的回调函数
3 广播执行的4个步骤
1.定义广播
2.注册广播
3.发送广播
4.接收广播
4 类图如下
5 代码如下
import java.util.ArrayList;
public class BroadcastSimulator {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("android 广播功能模拟");
Clinet c = new Clinet();
c.maind();
Clinet2 c2 = new Clinet2();
c2.maind();
}
}
abstract class BroadcastReceiver{
public abstract void onReceiver();
}
class Broadcaster{
public Broadcaster(String name,BroadcastReceiver b) {
this.name = name;
mBroadcastReceiver.add(b);
}
public String name;
public ArrayList<BroadcastReceiver> mBroadcastReceiver = new ArrayList<BroadcastReceiver>();
public void send() {
for(BroadcastReceiver b:mBroadcastReceiver) {
b.onReceiver();
}
}
}
class BroadcastManageerCenter{
public ArrayList<Broadcaster> mBroadcaster = new ArrayList<Broadcaster>();
void broadcast() {
}
public void registerReceiver(String str,BroadcastReceiver b) {
for(Broadcaster broadcaster:mBroadcaster) {
if(broadcaster.name.equals(str)) {
broadcaster.mBroadcastReceiver.add(b);
return;
}
}
mBroadcaster.add(new Broadcaster(str,b));
}
public void sendBroadcast(String broadcastName) {
for(Broadcaster broadcaster:mBroadcaster) {
if(broadcaster.name.equals(broadcastName)) {
broadcaster.send();
}
}
}
}
class LocalBroadcastManager extends BroadcastManageerCenter{
public static LocalBroadcastManager getInstance(Activity a) {
return a.getLocal();
}
}
class Activity{
public static BroadcastManageerCenter mBroadcastManageerCenter = null;
ClinetBase mClinetBase;
public Activity(ClinetBase c) {
mClinetBase = c;
if(mBroadcastManageerCenter == null) {
mBroadcastManageerCenter = new BroadcastManageerCenter();
}
}
protected void registerReceiver(String str,BroadcastReceiver b) {
mBroadcastManageerCenter.registerReceiver(str, b);
}
protected void sendsendBroadcast(String broadcastName) {
mBroadcastManageerCenter.sendBroadcast(broadcastName);
}
public LocalBroadcastManager getLocal() {
return mClinetBase.getLocal();
}
}
class ConcreteActivityReceiver extends Activity{
public ConcreteActivityReceiver(ClinetBase c) {
super(c);
}
String testBroadcast = "testBroadcast";
class MyBroadcastReceiver extends BroadcastReceiver{
public void onReceiver() {
System.out.println("ConcreteActivity 收到广播");
}
}
public void onCreate() {
MyBroadcastReceiver broadcast = new MyBroadcastReceiver();
registerReceiver(testBroadcast,broadcast);
LocalBroadcastManager.getInstance(this).registerReceiver(testBroadcast,broadcast);
}
}
class ConcreteActivitySend extends Activity{
public ConcreteActivitySend(ClinetBase c) {
super(c);
}
String testBroadcast = "testBroadcast";
public void sendBroadcast() {
System.out.println("ConcreteActivitySend 发送广播");
sendsendBroadcast(testBroadcast);
}
public void sendLocalBroadcast() {
System.out.println("ConcreteActivitySend 发送本地广播");
LocalBroadcastManager.getInstance(this).sendBroadcast(testBroadcast);
}
}
class ClinetBase{
LocalBroadcastManager local = null;
public LocalBroadcastManager getLocal() {
if(local == null) {
local = new LocalBroadcastManager();
}
return local;
}
}
class Clinet extends ClinetBase{
public Clinet() {}
void maind() {
ConcreteActivityReceiver receiver = new ConcreteActivityReceiver(this);
receiver.onCreate();
ConcreteActivitySend send = new ConcreteActivitySend(this);
System.out.println("\nClinet 发送全域广播");
send.sendBroadcast();
}
}
class Clinet2 extends ClinetBase{
public Clinet2() {}
void maind() {
ConcreteActivityReceiver receiver = new ConcreteActivityReceiver(this);
receiver.onCreate();
ConcreteActivitySend send = new ConcreteActivitySend(this);
System.out.println("\nClinet2 发送全域广播");
send.sendBroadcast();
System.out.println("\nClinet2 发送本地广播");
send.sendLocalBroadcast();
}
}
6 运行效果
广播的本质
广播的本质是什么呢,为什么任何应用,任何位置都能收到,说得具体点就是任何对象都能收到?
任何对象都能交互的语言特性是什么呢?static
相信我们为了夸对象的生命周期,也这么做过。
比如一个变量的有效范围是跨几个对象的生命周期,那么就得将这个变量变成静态成员。
所已广播也是如此,当广播发送时,任何监听的对象都能收到,就是因为这里有一个静态的监听列表,任何对象都可以注册。
发送广播时,就是通过这个静态的监听列表来完成。
静态是跨对象生命周期的基础,广播也不例外。