EventBus3.0配置及使用

一、前言

        EventBus是Android中组件与组件之间通信的框架,可以解耦和简化Activity与Activity、Activity和Fragment、Fragment和Fragment之间的通信。

        详情可移步至:

     github地址:https://github.com/greenrobot/EventBus
     官方文档:http://greenrobot.org/eventbus/documentation

EventBus主要角色:
  •  Event 传递的事件对象
  •  Subscriber  事件的订阅者 
  •  Publisher  事件的发布者
  •  ThreadMode 定义函数在何种线程中执行
各角色协作图:

    这里事件发布者将所有的事件都Post到EventBus这个总线,再通过EventBus将事件分发给对应的事件订阅者。

二、使用

1.首先需要依赖工程

compile 'org.greenrobot:eventbus:3.0.0'
如果是Eclipse工程则可以在官网下载对应jar包

2.使用前应该注册/订阅

EventBus.getDefault().register(this);
3.发布一个事件
EventBus.getDefault().post("message");
4.订阅事件处理
@Subscribe(threadMode = ThreadMode.MAIN,priority = 100) //在ui线程执行,优先级为100
public void onEvent(String message){
    Log.e(TAG, message);
}
ThreadMode总共四个:
 
  • MAIN 在UI主线程
  • BACKGROUND 后台线程,仅当发布者在UI线程时才会新建一个后台线程执行
  • POSTING 和发布者处在同一个线程
  • ASYNC 异步线程,无论发布者在哪个线程都重新建一个线程执行
优先级则为一个1000以内的整数

5.优先级高的可以拦截事件向下传递

EventBus.getDefault().cancelEventDelivery(event) ;
6.注销/解除订阅
EventBus.getDefault().unregister(this);
7.其他问题

这里也许我们会有一个疑问,就是怎么区分发送的事件由谁来接收呢?其实,我们post事件时发送了一个对象出去,那么接收的方法中传进来的参数也为这个对象的订阅者则会接收到这个消息。假如如果我们post的都是String的对象却想要不同的订阅者处理,则可以新建两个不同的类都包含一个String的信息来区分。即

public class Event {

    public static class A {} // A事件
    public static class B {} // B事件
    public static class C { // C事件
        String message;
        public C(String message) {
            this.message = message;
        }
    }

}
8.处理混淆
-keepattributes *Annotation*
-keepclassmembers class ** {
    @org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
# Only required if you use AsyncExecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
    <init>(java.lang.Throwable);
}

三、加速模式

EventBus3.0新增了一个加速模式,即通过相应的配置可以大大提高EventBus的性能。

1.app的build.gradle中的 dependencies 中还需要配置eventbus-annotation-processor

apt 'org.greenrobot:eventbus-annotation-processor:3.0.1'  
2.dependencies的下方添加
apt {
    arguments {
        eventBusIndex "com.afei.test.MyEventBusIndex"
    }
}
3.还要加上相应插件
apply plugin: 'com.neenbedankt.android-apt'
整体效果就是
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

android {
    compileSdkVersion 23
    buildToolsVersion "24.0.0"

    defaultConfig {
        applicationId "com.afei.test"
        minSdkVersion 17
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    apt 'org.greenrobot:eventbus-annotation-processor:3.0.1'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'org.greenrobot:eventbus:3.0.0'
    compile 'com.android.support:support-v4:23.4.0'
}

apt {
    arguments {
        eventBusIndex "com.afei.test.MyEventBusIndex"
    }
}
4.project build.gradle配置
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
5.设置完成后要记得rebuild一下,就可以成“MyEventBusIndex”这个类了

6.启用加速模式

EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();
最好实在Application启动的时候就调用这行代码保证之后所有的EventBus都默认使用了加速模式
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
EventBus是一种用于Android应用程序中发布/订阅事件的库。它遵循发布-订阅模式,允许不同组件之间进行松散耦合的通信。 在基于EventBus 3.0的APP开发中,你可以按照以下步骤进行: 1. 添加EventBus依赖 在项目的build.gradle文件中添加以下代码: ``` dependencies { implementation 'org.greenrobot:eventbus:3.2.0' } ``` 2. 创建事件类 创建一个事件类,它将包含你需要发送和接收的数据。例如: ``` public class MessageEvent { public final String message; public MessageEvent(String message) { this.message = message; } } ``` 3. 注册订阅者 在需要接收事件的组件中,注册订阅者。例如,在Activity中: ``` @Override public void onStart() { super.onStart(); EventBus.getDefault().register(this); } @Override public void onStop() { EventBus.getDefault().unregister(this); super.onStop(); } ``` 4. 发布事件 在需要发送事件的组件中,发布事件。例如,在Activity中: ``` EventBus.getDefault().post(new MessageEvent("Hello, world!")); ``` 5. 处理事件 在订阅者中,创建一个方法来处理接收到的事件。例如,在Activity中: ``` @Subscribe(threadMode = ThreadMode.MAIN) public void onMessageEvent(MessageEvent event) { // Do something with the event Toast.makeText(this, event.message, Toast.LENGTH_SHORT).show(); } ``` 以上就是基于EventBus 3.0的APP开发的基本步骤。通过使用EventBus,你可以轻松地在不同组件之间传递数据,从而实现应用程序中的松散耦合通信。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值