1、使用的时候有定义很多event类
2、event在注册的时候会调用反射去遍历注册对象的方法在其中找出带有@subscriber标签的方法,性能不高。
3、需要自己注册和反注册,如果忘了反注册就会导致内存泄漏
imlementation ‘org.greenrobot:eventbus:3.2.0’
-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);
}
public class EventMessage {
private int type;
private String message;
public EventMessage(int type, String message) {
this.type = type;
this.message = message;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return "type="+type+"--message= "+message;
}
}
注意事项:1,该方法有且仅有一个参数2,必须用public修饰,不能使用static或者abstract3,需要添加@Subscribe()注解
public class EventBusActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onStart() {
super.onStart();
//注册EventBus
EventBus.getDefault().register(this);
}
//接收事件
@Subscribe(threadMode = ThreadMode.POSTING, sticky = true, priority = 1)
public void onReceiveMsg(EventMessage message){
Log.e("EventBus_Subscriber", "onReceiveMsg_POSTING: " + message.toString());
}
//接收事件
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 1)
public void onReceiveMsg1(EventMessage message){
Log.e("EventBus_Subscriber", "onReceiveMsg_MAIN: " + message.toString());
}
//接收事件
@Subscribe(threadMode = ThreadMode.MAIN_ORDERED, sticky = true, priority = 1)
public void onReceiveMsg2(EventMessage message){
Log.e("EventBus_Subscriber", "onReceiveMsg_MAIN_ORDERED: " + message.toString());
}
//接收事件
@Subscribe(threadMode = ThreadMode.BACKGROUND, sticky = true, priority = 1)
public void onReceiveMsg3(EventMessage message){
Log.e("EventBus_Subscriber", "onReceiveMsg_BACKGROUND: " + message.toString());
}
//接收事件
@Subscribe(threadMode = ThreadMode.ASYNC, sticky = true, priority = 1)
public void onReceiveMsg4(EventMessage message){
Log.e("EventBus_Subscriber", "onReceiveMsg__ASYNC: " + message.toString());
}
@Override
protected void onDestroy() {
super.onDestroy();
//取消事件
EventBus.getDefault().unregister(this);
}
}
@OnClick(R2.id.send_event_common)
public void clickCommon(){
EventMessage message = new EventMessage(1, "这是一条普通事件");
EventBus.getDefault().post(message);
}
@OnClick(R2.id.send_event_sticky)
public void clickSticky(){
EventMessage message = new EventMessage(1, "这是一条黏性事件");
EventBus.getDefault().postSticky(message);
}
@Subscribe(threadMode = ThreadMode.MAIN,sticky = true,priority = 1)
public void onReceiveMsg(EventMessage message) {
Log.e(TAG, "onReceiveMsg: " + message.toString());
}
1、ThreadMode 模式
订阅处:
@Subscribe()
public void onReceiveMsg(EventMessage message) {
Log.e(TAG, "onReceiveMsg: " + message.toString());
Log.e(TAG, "onReceiveMsg: current thread name ="+Thread.currentThread().getName() );
}
发布处:
private View.OnClickListener mSendListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(TAG, "onClick: " );
new Thread(new Runnable() {
@Override
public void run() {
String name = Thread.currentThread().getName();
Log.e(TAG, "run: thread name = "+name );
EventMessage msg = new EventMessage(1,"Hello MainActivity");
EventBus.getDefault().post(msg);
}
}).start();
}
};
1.2 MAIN / MAIN_ODERED: 主线程接收事件
private View.OnClickListener mGoListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(TAG, "onClick: post");
EventMessage message = new EventMessage(233, "post message before");
EventBus.getDefault().postSticky(message);
}
};
private View.OnClickListener mRegisterListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(TAG, "onClick: start register" );
//当触发点击事件的时候,才进行注册,这个时候,可同样可以接收到上个点击事件中发出的 事件。
EventBus.getDefault().register(MainActivity.this);
}
};
3、priority
清晰讲解:EventBus核心原理其实保存这三张图就可以弄懂了,收藏一下 - 知乎
源码解析:EventBus 原理解析 - 简书