EventBus(一) 简单使用,不使用反射,使用注解处理器

系列文章

EventBus(一) 简单使用,不使用反射,使用注解处理器
EventBus(二)使用反射的方式的原理
EventBus(三)手写EventBus3.1.1

导入依赖

 // 依赖第三方库:EventBus
    ibloglementation 'org.greenrobot:eventbus:3.1.1'
    annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.1.1'

给EventBus注解处理器传参

 // 给注解处理器传参
        javaCoblogileOptions {
            annotationProcessorOptions {
                arguments = [ eventBusIndex : 'com.netease.eventbus.demo.MyEventBusIndex' ]
            }
        }

Application中初始化

/**
 * http://greenrobot.org/eventbus/documentation/subscriber-index/
 */
public class BaseApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();
    }
}

注册

public class MainActivity extends AppCoblogatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EventBus.getDefault().register(this);
    }

    // 订阅方法
    @Subscribe
    public void event(String string) {
        Log.e("event >>> ", string);
    }

    // 测试优先级
    @Subscribe(priority = 10, sticky = true)
    public void event2(String string) {
        Log.e("event2 >>> ", string);
    }

    // 点击事件
    public void jublog(View view) {
        startActivity(new Intent(this, SecondActivity.class));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}

发布事件

public class SecondActivity extends AppCoblogatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }

    // 点击事件
    public void post(View view) {
        // 发布事件
        // EventBus.getDefault().post("simon");
    }

}

粘性事件发送

public class MainActivity extends AppCoblogatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    // 点击事件
    public void jublog(View view) {
        EventBus.getDefault().postSticky("sticky");
        startActivity(new Intent(this, SecondActivity.class));
    }

}

接收粘性事件

/**
 * 未初始化 / 延时消费
 */
public class SecondActivity extends AppCoblogatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }

    // 点击事件
    public void post(View view) {
        // 注册
        EventBus.getDefault().register(this);
    }

    // 测试粘性事件
    @Subscribe(sticky = true)
    public void sticky(String string) {
        Log.e("sticky >>> ", string);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}

以上就是EventBus3.0的正确使用姿势,因为2.0的版本是用的反射实现的,所以如果按照官网的方式,使用的是反射的方式,如果要使用注解处理器的方式,可以通过上述的方式。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值