EventBus传值及生成索引


在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)自定义一个类,可以是空类,比如:

  1. public class AnyEventType {  
  2.      public AnyEventType(){}  
  3.  }  

(2)在要接收消息的页面注册:

  1. eventBus.register(this);  

(3)发送消息

  1. eventBus.post(new AnyEventType event);  

(4)接受消息的页面实现(共有四个函数,各功能不同,这是其中之一,可以选择性的实现,这里先实现一个):

  1. public void onEvent(AnyEventType event) {} 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值