在project的build.gradle下配置:
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' //EventBus配置
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
在App的 build.gradle下配置:
android {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments = [ eventBusIndex : 'com.example.myapp.MyEventBusIndex' ]
}
}
}
}
dependencies {
compile 'org.greenrobot:eventbus:3.0.0'
annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.0.1'
}
在MyApp extends Application中注册配置:
EventBus eventBus = EventBus.builder().addIndex(new MyEventBusIndex()).build();
MyEventBusIndex的文件必须在注册、订阅和发送完成的时候才会生成
(1)自定义一个类,可以是空类,比如:
- public class AnyEventType {
- public AnyEventType(){}
- }
(2)在要接收消息的页面注册:
- eventBus.register(this);
(3)发送消息
- eventBus.post(new AnyEventType event);
(4)接受消息的页面实现(共有四个函数,各功能不同,这是其中之一,可以选择性的实现,这里先实现一个):
- public void onEvent(AnyEventType event) {}