android 广播模拟-区分本地广播和全区域广播

一 广播知识总结
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
相信我们为了夸对象的生命周期,也这么做过。
比如一个变量的有效范围是跨几个对象的生命周期,那么就得将这个变量变成静态成员。
所已广播也是如此,当广播发送时,任何监听的对象都能收到,就是因为这里有一个静态的监听列表,任何对象都可以注册。
发送广播时,就是通过这个静态的监听列表来完成。
静态是跨对象生命周期的基础,广播也不例外。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值