Android观察者模式NotificationCenter,可以试试替换BroadCastReceiver
2016-08-30 15:32
429 查看
1、NotificationCenter
NotificationCenter是一个单例类,即与Director类一样。它主要用来管理订阅/发布消息的中心。
单例类的使用:通过 NotificationCenter.getInstance() 来获取单例对象。
它有三个核心函数和一个观察者数组:
订阅消息: addObserver()。订阅感兴趣的消息。
发布消息: postNotification()。发布消息。
退订消息: removeObserver()。不感兴趣了,就退订。
观察者数组: _observers
而观察者对象是NotificationObserver类,它的作用就是:将订阅的消息与相应的订阅者、订阅者绑定的回调函数联系起来。
(1)观察者需要implements NotificationCenterDelegate
(2)在界面开始(一般放在Oncreate方法里)和销毁里进行注册和移除(第一个参数一般传该acitivty对象(this))
(3)在didReceivedNotification里面判断标识(id)来进行处理object
(4)剩下的就是获取该单利对象发送消息了,哪块发送就写在哪块postNotificationName,这块的标识和注册、销毁里面的一样
个人感觉这个用起来感觉比Android的BroadCastRecevier好用简洁
NotificationCenter是一个单例类,即与Director类一样。它主要用来管理订阅/发布消息的中心。
单例类的使用:通过 NotificationCenter.getInstance() 来获取单例对象。
它有三个核心函数和一个观察者数组:
订阅消息: addObserver()。订阅感兴趣的消息。
发布消息: postNotification()。发布消息。
退订消息: removeObserver()。不感兴趣了,就退订。
观察者数组: _observers
而观察者对象是NotificationObserver类,它的作用就是:将订阅的消息与相应的订阅者、订阅者绑定的回调函数联系起来。
import java.util.ArrayList; import java.util.HashMap; /** * @Classname NotificationCenter.java * * @author chewei * * @Description 观察者模式 * * @Parameter */ public class NotificationCenter { final private HashMap<Integer, ArrayList<Object>> observers = new HashMap<Integer, ArrayList<Object>>(); final private HashMap<Integer, Object> removeAfterBroadcast = new HashMap<Integer, Object>(); final private HashMap<Integer, Object> addAfterBroadcast = new HashMap<Integer, Object>(); private boolean broadcasting = false; private static volatile NotificationCenter Instance = null; public static NotificationCenter getInstance() {//单例模式创建NotificationCenter NotificationCenter localInstance = Instance; if (localInstance == null) { synchronized (NotificationCenter.class) { localInstance = Instance; if (localInstance == null) { Instance = localInstance = new NotificationCenter(); } } } return localInstance; } public interface NotificationCenterDelegate {//接口用于通知观察者领取信息 public abstract void didReceivedNotification(int id, Object... args); } public void postNotificationName(int id, Object... args) {//被观察者发送消息 synchronized (observers) { broadcasting = true; ArrayList<Object> objects = observers.get(id); if (objects != null) { for (Object obj : objects) { ((NotificationCenterDelegate)obj).didReceivedNotification(id, args); } } broadcasting = false; if (!removeAfterBroadcast.isEmpty()) { for (HashMap.Entry<Integer, Object> entry : removeAfterBroadcast.entrySet()) { removeObserver(entry.getValue(), entry.getKey()); } removeAfterBroadcast.clear(); } if (!addAfterBroadcast.isEmpty()) { for (HashMap.Entry<Integer, Object> entry : addAfterBroadcast.entrySet()) { addObserver(entry.getValue(), entry.getKey()); } addAfterBroadcast.clear(); } } } public void addObserver(Object observer, int id) {//注册 synchronized (observers) { if (broadcasting) { addAfterBroadcast.put(id, observer); return; } ArrayList<Object> objects = observers.get(id); if (objects == null) { observers.put(id, (objects = new ArrayList<Object>())); } if (objects.contains(observer)) { return; } //if( id == MessagesController.contactsDidLoaded) //FileLog.d("emm", "postNotificationName addObserver"); objects.add(observer); } } public void removeObserver(Object observer, int id) {//移除 synchronized (observers) { if (broadcasting) { removeAfterBroadcast.put(id, observer); return; } ArrayList<Object> objects = observers.get(id); if (objects != null) { objects.remove(observer); if (objects.size() == 0) { observers.remove(id); } } } } }
(1)观察者需要implements NotificationCenterDelegate
(2)在界面开始(一般放在Oncreate方法里)和销毁里进行注册和移除(第一个参数一般传该acitivty对象(this))
(3)在didReceivedNotification里面判断标识(id)来进行处理object
(4)剩下的就是获取该单利对象发送消息了,哪块发送就写在哪块postNotificationName,这块的标识和注册、销毁里面的一样
个人感觉这个用起来感觉比Android的BroadCastRecevier好用简洁
相关文章推荐
- Do you WPS? 试试金山WPS, 应该可以替换你机器上的MS Office. 值得尝试!
- 电脑越用越慢怎么办?可以试试下面的方法
- 可以替换 UITextField 就可以达到校验目的的工具:US2FormValidator
- 在Android中自定义捕获Application全局异常,可以替换掉系统的强制退出对话框
- 当安卓官网打不开的时候可以试试这个网站
- 加密 解密(试试还可以)
- no server suitable for synchronization found 很简单,可以试试
- APP纯黑盒测试—某些可以试试的操作
- 15道海量数据处理的问题,牛人不牛人都可以试试!
- 给定一个只包含大写英文字母的字符串,可以用另一个字母替换字符串中的任何字母最多k次。查找包含所有重复字母的最长子字符串的长度,
- 好吧,终于可以试试了。
- Myeclipse9破解终于搞定了,找了很多都不行,无法自动生成System id 可以自动生成systemid 自动替换文件
- freemarker 替换字符串,模板,可以用来动态生成代码
- 练习1-10 编写一个将输入复制到输出的的程序,并将其中的制表符替换成\t,把回退符替换成\b,把反斜杠\替换成\\,这样可以将制表符和回退符以可见的方式显示出来。
- 用腻了bootstrap的可以试试semantic-ui
- 通达OA工作流引入任意工作流,可以替换父子流程,
- google2009年在华南地区的两道笔试题目,有兴趣的可以试试
- 去掉代码中的空行和注释行,可以使用以下正则表达式替换。
- 想修改.class文件?其实可以试试这样做!
- 在Android中自定义捕获Application全局异常,可以替换掉系统的强制退出对话框