开源地址:https://github.com/greenrobot/EventBus
compile 'org.greenrobot:eventbus:3.0.0'
public class MyEvent {
public String msg;
public MyEvent(String msg) {
this.msg = msg;
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册事件总线
EventBus.getDefault().register(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
//反注册事件总线
EventBus.getDefault().unregister(this);
}
/**
* POSTING线程模型:在哪个线程发布事件,就在哪个线程执行onPostingEvent方法
*/
@Subscribe(threadMode = ThreadMode.POSTING)
public void onPostingEvent(MyEvent event) {
Log.d(TAG, "onPostingEvent: " + Thread.currentThread().getName());
}
/**
* MAIN线程模型:不管是哪个线程发布事件,都在主线程执行onMainEvent方法
*/
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMainEvent(MyEvent event) {
Log.d(TAG, "onMainEvent: " + Thread.currentThread().getName());
}
/**
* BACKGROUND线程模型:事件如果是在子线程发布,onBackgroundEvent方法就在该子线程执行,事件如果是在
* 主线程中发布,onBackgroundEvent方法就在EventBus内部的线程池中执行
*/
@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onBackgroundEvent(MyEvent event) {
Log.d(TAG, "onBackgroundEvent: " + Thread.currentThread().getName());
}
/**
* ASYNC线程模型:不管事件在哪个线程发布,onAsyncEvent方法都在EventBus内部的线程池中执行
*/
@Subscribe(threadMode = ThreadMode.ASYNC)
public void onAsyncEvent(MyEvent event) {
Log.d(TAG, "onAsyncEvent: " + Thread.currentThread().getName());
}
/**
* 在主线程中发布事件
* @param view
*/
public void onPublishEventOnMainThread(View view) {
MyEvent event = new MyEvent("msg from publisher main thread");
EventBus.getDefault().post(event);
}
/**
* 在子线程中发送事件
* @param view
*/
public void onPublishEventOnBGThread(View view) {
new Thread(new Runnable() {
@Override
public void run() {
MyEvent event = new MyEvent("msg from publisher bg thread");
EventBus.getDefault().post(event);
}
}).start();
}
欢迎关注微信公众号