EventBus基本使用
来源:互联网 发布:java商城项目面试 编辑:程序博客网 时间:2024/06/03 19:41
1、引入
compile 'org.greenrobot:eventbus:3.0.0'
2、定义事件实体类
public class EventMsg { private String msg; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; }}
3、注册事件和释放事件
EventBus.getDefault().register(this); // 订阅
@Overrideprotected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); // 解除订阅}
4、订阅事件处理
@Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行public void onDataSynEvent(EventMsg event) { tvShowMsg.setText(event.getMsg());}
5、发送消息
EventMsg msg = new EventMsg();msg.setMsg("hello eventBus");EventBus.getDefault().post(msg);
6、订阅事件的优先级
@Subscribe(threadMode = ThreadMode.MAIN,priority = 100) //在ui线程执行 优先级100public void onDataSynEvent(EventMsg event) { Log.e(TAG, "event---->" + event.getCount());}
7、终止事件往下传递
EventBus.getDefault().cancelEventDelivery(event) ;//优先级高的订阅者可以终止事件往下传递
8、总结
以上是EventBus的最简单实用方法,3、4点代码一般放在同一个activity中,起到注册监听的作用,5点放在另一个activity中,主要用来发送事件。
接下来将从源码的角度来分析 getDefault() –>register()–>post()–>unregister()这四个过程的具体实现。
阅读全文
0 0
- Eventbus的基本使用
- Android EventBus 基本使用
- EventBus基本使用简介
- EventBus的基本使用
- eventbus基本使用
- EventBus的基本使用
- EventBus基本使用
- EventBus基本使用
- Android EventBus的基本使用
- Android:EventBus-3 基本使用
- 深入理解EventBus - 基本使用
- EventBus(1)——基本使用
- EventBus深入浅析之基本使用
- EventBus 基本使用和进阶配置
- EventBus的基本使用及原理
- Android的EventBus和butterknife基本使用
- EventBus源码详解(一):基本使用
- EventBus完全解析(一)基本使用
- Javascript调试函数console使用
- MongoDB入门1
- 微博分析报告学习
- Linux 目录结构及文件基本操作
- 1033. 旧键盘打字(20)
- EventBus基本使用
- xamarin学习笔记A12(安卓Notification)
- 安装步骤
- C++ 读取raw文件并保存为到bmp图片上
- 300.32实现网页注册
- 使用Spring boot 快速 搭建 OAuth2 Server
- “第二盖茨”——马克·扎克伯格
- Spring MVC配置详解
- 网狐荣耀版李奎劈鱼游戏,捕鱼游戏提示“非法组件的解决办法”