大家都知道在iOS中Controller之间传递数据最常用的就是delegate和通知了,特别是通知,可以很方便在任何对象直接传递数据,而且是很好的解耦设计.  在Android之中 传递数据一般用Intent的data功能, 但是如果数据传递比较复杂,这就比较麻烦. 在Android中有广播组建,但是一般用于应用之间,效率比较低.而且做界面更新的时候也比较困难. 今天我要给大家介绍的是一个Android版本的Notification,使用的api和iOS的 Notification几乎一样.

1.注册通知

//向通知中心注册通知          NotificationCenter.getInstance().addObserver(this,this.getClass().getMethod("onNotification",Notification.class),"NotificationName",null);//当监听到通知public void onNotification(Notification notification){       System.out.println("接收到通知"+notification);       //TODO 界面更新和其他处理 } //当Activity销毁时从通知中心取消注册@Overrideprotected void onDestroy() {super.onDestroy();    NotificationCenter.getInstance().removeObserver(this); }

2.发送通知

//发布通知Map<String,Object> userInfo = new HashMap<>();userInfo.put("key","value");NotificationCenter.getInstance().postNotification(this,"NotificationName",userInfo);
  1. github 下载地址

更多相关文章

  1. mybatisplus的坑 insert标签insert into select无参数问题的解决
  2. python起点网月票榜字体反爬案例
  3. [置顶] android adapter
  4. 【android】android Handler应用详解
  5. Android主流三方库源码分析(四、深入理解GreenDao源码)
  6. android的URI学习
  7. android 连接远程数据库
  8. 开发自己的监控系统三、移动篇(android)
  9. Android[中级教程]第八章 Json数据的处理

随机推荐

  1. Android常用设计模式(二)
  2. Android(安卓)单元测试和日志输出
  3. [置顶] android activity返回键被dialog
  4. Android应用启动后自动创建桌面快捷方式
  5. Android中的消息机制
  6. 获取Nexus系列手机驱动(用于编译Android(
  7. Google将在Android 2.2中提供Adobe Flash
  8. Android add prebuilt lib(*.so) to Andr
  9. Android多媒体学习一:Android中Image的简
  10. Android下使用Hessian与Java服务端通讯