android中使用EventBus进行消息通知
2015-08-13 17:15
429 查看
// ------------ 接收消息类,(注册,注销,处理消息)----------
// 注册
EventBus.getDefault().register(this);
// 处理,必须是onEventMainThread(param) para为消息实体类
public void onEventMainThread(MsgBody msg) {
if (msg.getType() == MsgBody.TYPE_1) {
// TODO
}
}
// 注销
EventBus.getDefault().unregister(this);
// ----------- 发送消息类 --------------
EventBus.getDefault().post(new MsgBody(MsgBody.TYPE_1));
// 消息实体类
public class MsgBody{
public static final int TYPE_1 = 1;
private int type;
public MsgBody(int type) {
this.type = type;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
// 注册
EventBus.getDefault().register(this);
// 处理,必须是onEventMainThread(param) para为消息实体类
public void onEventMainThread(MsgBody msg) {
if (msg.getType() == MsgBody.TYPE_1) {
// TODO
}
}
// 注销
EventBus.getDefault().unregister(this);
// ----------- 发送消息类 --------------
EventBus.getDefault().post(new MsgBody(MsgBody.TYPE_1));
// 消息实体类
public class MsgBody{
public static final int TYPE_1 = 1;
private int type;
public MsgBody(int type) {
this.type = type;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
相关文章推荐
- 1、Android开发之四种点击事件
- Android4.4 蓝牙源代码段分析
- 用一个textview显示数字,让数字从一个值渐变到另外一个值
- Android Layout_weight属性完全解析以及使用ListView来实现表格
- android Fragment和Activity
- 将cordova集成到Android studio的最佳方法
- Ubuntu搭建Android交叉编译环境
- Android分享功能的一点总结
- Android笔记:四大组件
- init进程 && 解析Android启动脚本init.rc && 修改它使不启动android && init.rc中启动一个sh文件
- android 启动程序的一些标准方法
- Android(java)学习笔记150:为什么局部内部类只能访问外部类中的 final型的常量
- android textview html font标签不好用
- hdoj-5099-Comparison of Android versions
- android获取/更改gps和WIFI状态
- Android:ViewDragHelper学习
- Android中BaseAdapter的理解
- [置顶] android源码编译apk集成第三方so库
- android视图切换库SwitchLayout的使用
- android的recovery模式