EventBus是一款针对Android优化的发布/订阅事件总线。简化了应用程序内各组件间、组件与后台线程间的通信。优点是开销小,代码更优雅,以及将发送者和接收者解耦。如果Activity和Activity进行交互还好说,如果Fragment和Fragment进行交互着实令人头疼,其可简化 Activities, Fragments, Threads, Services 等组件间的消息传递,可替代 Intent、Handler、BroadCast接口等传统方案。
gradle如何添加支持:
compile 'org.greenrobot:eventbus:3.0.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);
}
(1)自定义一个事件类
public class MessageEvent {
...
}
(2)在需要订阅事件的地方注册事件
EventBus.getDefault().register(this);
(3)发送事件
EventBus.getDefault().post(messageEvent);
(4)处理事件
@Subscribe(threadMode = ThreadMode.MAIN)
public void XXX(MessageEvent messageEvent) {
...
}
消息处理的方法可以随便取名,但是需要添加一个注解@Subscribe,并且要指定线程模型(默认为POSTING)。
(5)取消事件订阅
EventBus.getDefault().unregister(this);
//定义消息类
public class MessageEvent {
private int code;
private String result;
public MessageEvent() {
}
public MessageEvent(int code, String result) {
this.code = code;
this.result = result;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}
public class EventBusActivity1 extends AppCompatActivity {
public static final int MSG_WHAT1 = 1;
public static final int MSG_WHAT2 = 2;
//普通数据
@Subscribe(threadMode = ThreadMode.MAIN)
public void dealEventMessage(MessageEvent messageEvent) {
switch (messageEvent.getCode()) {
//处理消息1
case MSG_WHAT1:
Toast.makeText(this, messageEvent.getResult() + MSG_WHAT1, Toast.LENGTH_SHORT).show();
break;
//处理消息2
case MSG_WHAT2:
Toast.makeText(this, messageEvent.getResult() + MSG_WHAT2, Toast.LENGTH_SHORT).show();
break;
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event_bus_activity);
//注册
EventBus.getDefault().register(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
//取消注册
EventBus.getDefault().unregister(this);
}
//发送消息1
public void sendBtn(View view) {
EventBus.getDefault().post(new MessageEvent(MSG_WHAT1, "Activity1发送"));
}
//发送消息2
public void startActivity(View view) {
Intent intent = new Intent(this, EventBusActivity2.class);
startActivity(intent);
}
}
//发送消息2的逻辑
public class EventBusActivity2 extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
EventBus.getDefault().post(new MessageEvent(EventBusActivity1.MSG_WHAT2, "Activity2发送"));
finish();
}
}
public class EventBusActivity1 extends AppCompatActivity {
public static final int MSG_WHAT3 = 3;
//粘性事件
@Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
public void dealStickyEvent(MessageEvent messageEvent) {
switch (messageEvent.getCode()) {
case MSG_WHAT3:
Toast.makeText(this, messageEvent.getResult() + MSG_WHAT3, Toast.LENGTH_SHORT).show();
break;
}
}
//(1)先点击发送,可多次点击,只收到最后一次发送事件
public void sendBtn(View view) {
EventBus.getDefault().postSticky(new MessageEvent(MSG_WHAT3, "粘性事件发送"));
}
//(2)后点击注册
public void segisterBtn(View view) {
EventBus.getDefault().register(this);
}
}
EventBus的基本使用我们就说到这里,下节我们说一下EventBus的4中模式EventBus使用详解(二)。