Skip to content

YooTrepeat/EventBusDemo

 
 

Repository files navigation

EventBus使用

开源地址:https://github.com/greenrobot/EventBus

使用步骤

1. 在Module的build.gradle添加依赖

compile 'org.greenrobot:eventbus:3.0.0'

2. 创建事件

public class MyEvent {
    public String msg;
    public MyEvent(String msg) {
        this.msg = msg;
    }
}

3. 注册和反注册EventBus

@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);
}

4. 监听事件

/**
 * 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());
}

5. 发布事件

/**
 * 在主线程中发布事件
 * @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();
}

欢迎关注微信公众号

About

EventBus基本的使用姿势

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%