EventBus3.0的使用

注:此文章转载至 “简书” 作者:JellyCai 原文链接:http://www.jianshu.com/p/72f475ac3a8d


在使用EventBus之前需先添加依赖:

compile 'org.greenrobot:eventbus:3.0.0'

最基本用法:

传递数据


接收数据



接收数据之前需要在接收数据的组件里  初始化注册     在组件销毁时注销注册

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    message = (TextView) this.findViewById(R.id.message);
    EventBus.getDefault().register(this);
}

创建方法接受数据 并注明@Subscribe  , 将接收的数据以参数的形式传入  注意:参数只能是对象,不能是基本数据类型

2.在结束的时候注销事件:

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


更多用法:

线程控制

在EventBus中有4种线程控制的方式:

1.POSTING:同步模式,顾名思义只中方式就是接收事件方法的线程和发送事件方法的线程一致,如果发送事件是在主线程中,那么接收事件也是在主线程中。如果发送事件的是在子线程,那么那么接收事件也会发送事件的子线程执行。总之会保持一致。

2.MAIN:主线程模式,无论发送事件是在那个线程发送,接收事件一定会在主线程中执行。这样刚好就解决了只能在主线程中更新UI的问题。

3.BACKGROUND:后台线程模式,如果发送事件的是在主线程中发送,接收事件就会在新建一个子线程中执行。发送事件是在子线程中执行,接收事件就会发发送事件的子线程中执行。这种模式适合处理耗时任务。

4.ASYNC:新线程模式,无论发送事件是在何种线程执行,接收事件一定会新建一个子线程去接收。

线程控制代码示例:

事件接收代码,在每一个线程模式里面打印了当前线程的Name,以下为了编码方便,所有代码均在一个Activity里面完成:

@Subscribe(threadMode = ThreadMode.MAIN)
public void onBusMain(String message){
    Log.v("bus","main"+Thread.currentThread().getName());
}

@Subscribe(threadMode = ThreadMode.POSTING)
public void onBusPosting(String message){
    Log.v("bus","posting"+Thread.currentThread().getName());
}

@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onBusBackground(String message){
    Log.v("bus","background"+Thread.currentThread().getName());
}

@Subscribe(threadMode = ThreadMode.ASYNC)
public void onBusAsync(String message){
    Log.v("bus","async"+Thread.currentThread().getName());
}

在界面上加入两个按钮:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="mainThread"
        android:text="在主线程发送"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="sonThread"
        android:text="在子线程发送"
        />

</LinearLayout>

按钮的点击事件,一个在主线程发出事件,一个在子线程里面发出事件:

public void mainThread(View view){
    EventBus.getDefault().post("bus");
}

public void sonThread(View view){
    new Thread(new Runnable() {
        @Override
        public void run() {
            EventBus.getDefault().post("bus");
        }
    }).start();
}

点击主线程按钮,logcat打印结果:

09-30 15:05:13.296 18777-18777/com.jelly.eventbus V/bus: mainmain
09-30 15:05:13.296 18777-18777/com.jelly.eventbus V/bus: postingmain
09-30 15:05:13.296 18777-26381/com.jelly.eventbus V/bus: backgroundpool-1-thread-2
09-30 15:05:13.296 18777-26380/com.jelly.eventbus V/bus: asyncpool-1-thread-1

点击子线程按钮,locat打印结果:

09-30 15:23:11.628 11282-11282/com.jelly.eventbus V/bus: mainmain    
09-30 15:23:11.616 11282-11392/com.jelly.eventbus V/bus: postingThread-220
09-30 15:23:11.616 11282-11392/com.jelly.eventbus V/bus: backgroundThread-220
09-30 15:23:11.616 11282-11394/com.jelly.eventbus V/bus: asyncpool-1-thread-1

从上面的打印结果来看,可以论述上面的四点结论。

优先级

EventBus可以通过设置每个接收事件方法的优先级@Subscribe(priority = 1)开控制接收方法的先后,实例代码:

@Subscribe(threadMode = ThreadMode.MAIN,priority = 1)
public void onBus1(String msg){
    Log.v("bus",1 + msg);
}

priority的值越大,接收顺序就越靠前。如果指定俩个方法的priority的值为1和2,那么priority为2的先接收到,为1的后接收到,还可以在方法内通过cancelEventDelivery()截断事件的传递。实例代码:

package com.jelly.eventbus;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by Jelly on 2016/9/30.
 */

public class PriorityActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_priority);
        EventBus.getDefault().register(this);
    }

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


    @Subscribe(threadMode = ThreadMode.MAIN,priority = 0)
    public void onBus(String msg){
        Log.v("bus",0 + msg);
    }

    @Subscribe(threadMode = ThreadMode.MAIN,priority = 1)
    public void onBus1(String msg){
        Log.v("bus",1 + msg);
    }

    @Subscribe(threadMode = ThreadMode.POSTING,priority = 2)
    public void onBus2(String msg){
        Log.v("bus",2 + msg);
    }

    @Subscribe(threadMode = ThreadMode.MAIN,priority = 3)
    public void onBus3(String msg){
        Log.v("bus",3 + msg);
    }



    public void click(View view){
        EventBus.getDefault().post("消息");
    }

}

没有加取消事件的logcat结果:

10-04 15:58:25.505 4614-4614/com.jelly.eventbus V/bus: 3消息
10-04 15:58:25.505 4614-4614/com.jelly.eventbus V/bus: 2消息
10-04 15:58:25.505 4614-4614/com.jelly.eventbus V/bus: 1消息
10-04 15:58:25.505 4614-4614/com.jelly.eventbus V/bus: 0消息

现在加上取消事件传递:

@Subscribe(threadMode = ThreadMode.POSTING,priority = 2)
public void onBus2(String msg){
    Log.v("bus",2 + msg);
    EventBus.getDefault().cancelEventDelivery(msg);
}

打印结果:

10-04 15:55:00.685 4614-4614/com.jelly.eventbus V/bus: 3消息
10-04 15:55:00.685 4614-4614/com.jelly.eventbus V/bus: 2消息

在取消事件传递的时候有一个注意点,在取消事件传递的方法的线程Mode必须是POSTING的,不然会报event handlers may only abort the incoming event的异常,可以从cancelEventDelivery的源码中看到,在下面代码中的最后一个判断,如果当前事件接收方法不是POSTING,就会抛出这个异常:

    public void cancelEventDelivery(Object event) {
    PostingThreadState postingState = currentPostingThreadState.get();
    if (!postingState.isPosting) {
        throw new EventBusException(
                "This method may only be called from inside event handling methods on the posting thread");
    } else if (event == null) {
        throw new EventBusException("Event may not be null");
    } else if (postingState.event != event) {
        throw new EventBusException("Only the currently handled event may be aborted");
    } else if (postingState.subscription.subscriberMethod.threadMode != ThreadMode.POSTING) {
        throw new EventBusException(" event handlers may only abort the incoming event");
    }

    postingState.canceled = true;
}

黏性事件

EventBus支持粘性事件,粘性事件就是在发送了事件之后,再订阅事件,而不是在发送事件之前订阅,事件接收方法也能收到,通过@Subscribe(sticky = true)去指定,发送事件必须通过postSticky发送。示例代码:

package com.jelly.eventbus;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by Jelly on 2016/10/4.
 */

public class StickyActivity extends Activity{

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

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

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onBus(String msg){
        Log.v("bus",msg);
    }

    public void click(View view){
        EventBus.getDefault().postSticky("消息");
        EventBus.getDefault().register(this);
    }

}

打印结果:

10-04 16:26:43.401 2405-2405/com.jelly.eventbus V/bus: 消息

效率提升

在EventBus3.0中加入了EventBusAnnotationProcessor提升速度:
1.在项目的gradle的dependencies中引入apt,加入classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8',结果如下:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

2.在app module的build.gradle中应用apt插件,并设置apt生成的索引的包名和类名,加入以下代码:

apply plugin: 'com.neenbedankt.android-apt'

apt {
    arguments {
        eventBusIndex "com.jelly.eventbus.MyEventBusIndex"
    }
}

apt 'org.greenrobot:eventbus-annotation-processor:3.0.1'

结果如下:

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    defaultConfig {
        applicationId "com.jelly.eventbus"
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

apt {
    arguments {
        eventBusIndex "com.jelly.eventbus.MyEventBusIndex"
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:23.4.0'
    testCompile 'junit:junit:4.12'
    compile 'org.greenrobot:eventbus:3.0.0'
    apt 'org.greenrobot:eventbus-annotation-processor:3.0.1'
}

然后重新编译,记住一定要重新编译,不然找不到MyEventBusIndex,然后在MyApplication里面初始化Index:

EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();


文/JellyCai(简书作者)
原文链接:http://www.jianshu.com/p/72f475ac3a8d
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。


注:此文章转载至 “简书”  原文链接:http://www.jianshu.com/p/72f475ac3a8d
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值