// ------------ 接收消息类,(注册,注销,处理消息)----------
// 注册
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;
}
}