目标
1: 集成EventBus
2: 实现简单的使用
3: 粘性事件
1: 集成
添加 implementation 'org.greenrobot:eventbus:3.1.1'
2: 简单使用
目标
1: 创建项目, 编写两个 Activity(以下简称A1,A2)
2: A1布局
按钮 button -> 跳转到A2
文本 textView -> 显示 A2 发送的消息
A2布局
按钮 button -> 发送消息
3: 编写自己的消息体(自定义类), 用来承载消息内容
###混淆
首先,因为EventBus 3弃用了反射的方式去寻找回调方法,改用注解的方式。作者的意思是在混淆时就不用再keep住相应的类和方法。但是我们在运行时,却会报java.lang.NoSuchFieldError: No static field POSTING。网上给出的解决办法是keep住所有eventbus相关的代码:
-keep class de.greenrobot.** {*;}
其实我们仔细分析,可以看到是因为在SubscriberMethodFinder的findUsingReflection方法中,在调用Method.getAnnotation()时获取ThreadMode这个enum失败了,所以我们只需要keep住这个enum就可以了(如下)。
-keep public enum org.greenrobot.eventbus.ThreadMode { public static *; }
这样就能正常编译通过了,但如果使用了索引加速,是不会有上面这个问题的.
因为在找方法时,调用的不是findUsingReflection,而是findUsingInfo。但是使用了索引加速后,编译后却会报新的错误:
Could not find subscriber method in XXX Class. Maybe a missing ProGuard rule?
这就很好理解了,因为生成索引GeneratedSubscriberIndex是在代码混淆之前进行的,混淆之后类名和方法名都不一样了(上面这个错误是方法无法找到),得keep住所有被Subscribe注解标注的方法:
-keepclassmembers class * {
@de.greenrobot.event.Subscribe <methods>;
}
所以又倒退回了EventBus2.4时不能混淆onEvent开头的方法一样的处境了。所以这里就得权衡一下利弊:使用了注解不用索引加速,则只需要keep住EventBus相关的代码,现有的代码可以正常的进行混淆。而使用了索引加速的话,则需要keep住相关的方法和类。
代码
A1 布局
<Button
android:id="@+id/go_to"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转第二页面"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar"/>
<TextView
android:id="@+id/tv"
android:text="@string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/go_to"/>
A1代码
在 onCreate()方法中
//1: 注册
EventBus.getDefault().register(this);
//2: 跳转第二个页面
busBinding.goTo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goToActivity(BusTwoActivity.class);
}
});
// 3:接受消息方法 BusType为我们自定义消息体
public void onEventMainThread(BusType event) {
busBinding.tv.setText( event.getMsg());
ToastUtil.shortToast(context, event.getMsg());
}
//3.1: 另一种方法
// 注解方式 主线程接受消息
@Subscribe(threadMode = ThreadMode.MAIN)
public void MyEvent(MessageEvent messageEvent) {
busBinding.tv.setText( event.getMsg());
ToastUtil.shortToast(context, event.getMsg());
}
// 4:在 onDestroy()方法中解除注册
if(EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().unregister(this);
}
A2布局
<Button
android:id="@+id/send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送消息"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar" />
A2代码
twoBinding.send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//发送消息
EventBus.getDefault().post(new BusType("这是第二个页面发送的消息"));
}
});
定义 消息体BusType (类似于 JavaBean)
private String mMsg;
public BusType(String msg) {
mMsg = msg;
}
public String getMsg(){
return mMsg;
}
3: 粘性事件
何为黏性事件呢?简单讲,就是在发送事件之后再订阅该事件也能收到该事件。Android中就有这样的实例,也就是Sticky Broadcast,即粘性广播。正常情况下如果发送者发送了某个广播,而接收者在这个广播发送后才注册自己的Receiver,这时接收者便无法接收到 刚才的广播,为此Android引入了StickyBroadcast,在广播发送结束后会保存刚刚发送的广播(Intent),这样当接收者注册完 Receiver后就可以接收到刚才已经发布的广播。这就使得我们可以预先处理一些事件,让有消费者时再把这些事件投递给消费者.
EventBus也提供了这样的功能,有所不同是EventBus会存储所有的Sticky事件,如果某个事件在不需要再存储则需要手动进行移除。用户通过Sticky的形式发布事件,而消费者也需要通过Sticky的形式进行注册,当然这种注册除了可以接收 Sticky事件之外和常规的注册功能是一样的,其他类型的事件也会被正常处理。
发送事件
EventBus.getDefault().postSticky("RECOGNIZE_SONG");
接受事件
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void receiveSoundRecongnizedmsg(String insType) {
if ("RECOGNIZE_SONG".equals(insType)) {
soundRecognizeCtrl();
}
}
移除
MessageEvent stickyEvent = EventBus.getDefault().getStickyEvent(MessageEvent.class);
if(stickyEvent != null) {
EventBus.getDefault().removeStickyEvent(stickyEvent);
//TODO
}
或者
MessageEvent stickyEvent = EventBus.getDefault().removeStickyEvent(MessageEvent.class);
if(stickyEvent != null) {
// Now do something with it
}