EventBus 3.0的简单使用

0.在build.gradle中引入

compile 'org.greenrobot:eventbus:3.0.0'

1.定义事件

public static class MessageEvent { /* Additional fields if needed */ }

2.注册订阅

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

3.取消注册

  @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

4.声明订阅方法

/***
*ThreadMode总共四个:
*NAIN UI主线程
*BACKGROUND 后台线程
*POSTING 和发布者处在同一个线程
*ASYNC 异步线程
*priority  优先级越高优先获得消息
*/
@Subscribe(threadMode = ThreadMode.MAIN,priority = 100)  
public void onMessageEvent(MessageEvent event) {/* Do something */};

5.订阅事件

EventBus.getDefault().post(new MessageEvent());

6.添加混淆

-keepattributes *Annotation*
-keepclassmembers class ** {
    @org.greenrobot.eventbus.Subscribe ;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }

# Only required if you use AsyncExecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
    (java.lang.Throwable);
}

你可能感兴趣的:(EventBus 3.0的简单使用)