1.在app的build文件中添加引用
compile 'org.greenrobot:eventbus:3.0.0'
2.写一个简单的消息类
3.发送消息
EventBus.getDefault().post(new MessageEvent(MessageCollectUtil.ADD_EXPERT_ATTENTION_SUGGEST));
4.注册广播
//注册EventBus
EventBus.getDefault().register(this);
//反注册EventBus
EventBus.getDefault().unregister(this);
5.处理消息
@Subscribe
public void onEventMainThread(MessageEvent event) {
//Log.d("tag", "onEventMainThread收到了消息:" + event.getMsg());
if (event.getMsg().equals(MessageCollectUtil.ADD_EXPERT_ATTENTION_CHANGE)) {
onRefresh();
}
}
其中,messageCollectUtil的内容如下:
package com.example.eventmessage;
public class MessageCollectUtil {
public static String UPDATE_TOUXIANG_MESSAGE = "touxiang change";
}