EventBus

发送事件的类不需要注册和反注册

要在下一个界面注册并且在打开界面之后立马发送消息将接受不到,原因是太快了还没来得及注册就发送了。

解决方法:使用postSticky粘性发送消息,即在注册之前发送消息也能收到消息

并且在回调方法注解里面添加sticky =true

startActivity(new Intent(this,LoginActivity.class));
            OthersLogin event = new OthersLogin(true);
            LogUtil.showLog("eeee=publish","pppppppp");
            EventBus.getDefault().postSticky(event);
            finish();
            
  @Subscribe(threadMode = ThreadMode.POSTING,sticky =true)
//    @Subscribe(sticky =true)
public void onPostingEvent(OthersLogin event) {
    othersLogin=event.othersClick;
    
}

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();
}
    原文作者:喂_balabala
    原文地址: https://www.jianshu.com/p/36baa20624df
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞