1.EventBus概述
EventBus是一款针对Android优化的发布/订阅事件总线。简化了应用程序内各组件间、组件与后台线程间的通信。优点是开销小,代码更优雅,以及将发送者和接收者解耦。可以很方便的在Activity,service,Fragment之间进行交互和通信。
EventBus有三个主要的元素需要我们先了解一下:
EventBus3.0有以下四种ThreadMode:
1)自定义一个事件类
public class MessageEvent {
...
}
EventBus.getDefault().register(this);
扫描了当前类中的所有方法,并将以@Subscribe注解的订阅方法存储在Map中,键就是参数的Class类型,value就是订阅方法的list集合
EventBus.getDefault().post(messageEvent);
根据Post传入的参数,去找到匹配的订阅方法,通过反射并调用之。
@Subscribe(threadMode = ThreadMode.MAIN)
public void XXX(MessageEvent messageEvent) {
...
}
Post之后,订阅方法开始执行,并有四种执行的ThreadMode
EventBus.getDefault().unregister(this);
Android Studio 配置gradle:
compile 'org.greenrobot:eventbus:3.0.0'
public class MessageEvent {
private String message;
public MessageEvent(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public class MainActivity extends AppCompatActivity {
private TextView tv_message;
private Button bt_message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_message=(TextView)this.findViewById(R.id.tv_message);
tv_message.setText("MainActivity");
bt_message=(Button)this.findViewById(R.id.bt_message);
bt_message.setText("跳转到SecondActivity");
bt_message.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,SecondActivity.class));
}
});
//注册事件
EventBus.getDefault().register(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
//取消注册事件
EventBus.getDefault().unregister(this);
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMoonEvent(MessageEvent messageEvent){
tv_message.setText(messageEvent.getMessage());
}
public class SecondActivity extends AppCompatActivity {
private Button bt_message;
private TextView tv_message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_message=(TextView)this.findViewById(R.id.tv_message);
tv_message.setText("SecondActivity");
bt_message=(Button)this.findViewById(R.id.bt_message);
bt_message.setText("发送事件");
bt_message.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post(new MessageEvent("欢迎关注刘望舒的博客"));
finish();
}
});
}
}
接下来我们点击按钮进入SecondActivity并点击该界面中的发送事件按钮,这时SecondActivity被finish掉,MainActivity的TextView显示
4.粘性事件
除了上面讲的普通事件外,EventBus还支持发送黏性事件,就是在发送事件之后再订阅该事件也能收到该事件,跟黏性广播类似。
bt_subscription.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//注册事件
EventBus.getDefault().register(MainActivity.this);
}
});
@Subscribe(threadMode = ThreadMode.POSTING,sticky = true)
public void ononMoonStickyEvent(MessageEvent messageEvent){
tv_message.setText(messageEvent.getMessage());
}
bt_subscription.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().postSticky(new MessageEvent("粘性事件"));
finish();
}
});
接下来我们点击订阅事件,TextView发生改变显示“粘性事件”:
5 .ProGuard 混淆规则
-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);
}