继承类:
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
一、BaseActivity的onCreate方法里调用:EventBus.getDefault().register(this);
二、BaseActivity的onDestroy方法里销毁:EventBus.getDefault().unregister(this);
三:新建类MessageEvent
public class MessageEvent implements Serializable {
private static final long serialVersionUID = -8435896567844807539L;
int flag;
Object obj;
public MessageEvent(int flag) {
this.flag = flag;
}
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
public MessageEvent(Object obj) {
this.obj = obj;
}
public MessageEvent(int flag, Object obj) {
this.flag = flag;
this.obj = obj;
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
}
四、定义实现方法
@Subscribe(threadMode = ThreadMode.MAIN)
public void messageEventBusMain(MessageEvent event) {
}
@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void messageEventBusBackground(MessageEvent event) {
}
五:发送 EventBus.getDefault().post(new MessageEvent(131));
六:收取:
@Override
public void messageEventBusMain(MessageEvent event) {
int flag = event.getFlag();
if (flag == 131) {
//这里就是获取到后自己的逻辑
}
}