1.把EvenBus3.0导入依赖
build.gradle添加引用
compile 'org.greenrobot:eventbus:3.0.0'
2.构造消息类,EvenBus订阅事件Object的子类
public class MessageEven {
private String message;
public MessageEven(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
3.在activity_main里面增加几个按钮 和一个TextView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.yaya.eventbusdemo.MainActivity">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="@+id/button"/>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button"
android:id="@+id/button2"/>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_alignRight="@+id/button2"
android:layout_alignEnd="@+id/button2"
android:id="@+id/button3"/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="44dp"
android:layout_marginStart="44dp"
android:layout_marginTop="72dp"
android:id="@+id/textView2"/>
</RelativeLayout>
4.MainActivity中的代码
package com.yaya.eventbusdemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
@Bind(R.id.button)
Button mButton;
@Bind(R.id.button2)
Button mButton2;
@Bind(R.id.button3)
Button mButton3;
@Bind(R.id.textView2)
TextView mTextView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
EventBus.getDefault().register(this);
}
@OnClick({R.id.button, R.id.button2, R.id.button3})
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:
EventBus.getDefault().post(new MessageEven("hello"));
break;
case R.id.button2:
startActivity(new Intent(this,SecondActivity.class));
break;
case R.id.button3:
break;
}
}
/**
* 主线程中执行
* @param msg
*/
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMainEventBus(MessageEven even) {
mTextView2.setText(even.getMessage());
Log.e("===Main", Thread.currentThread().getName());
}
/**
* 后台线程
* @param msg
*/
@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onBackgroundEventBus(MessageEven even) {
Log.e("===OnBack", Thread.currentThread().getName());
}
/**
* 异步线程
*
* @param msg
*/
@Subscribe(threadMode = ThreadMode.ASYNC)
public void onAsyncEventBus(MessageEven even) {
Log.e("===onAsy", Thread.currentThread().getName());
}
/**
* 默认情况,和发送事件在同一个线程
*
* @param msg
*/
@Subscribe(threadMode = ThreadMode.POSTING,priority = 100)
public void onPostEventBus(MessageEven even) {
Log.e("===onPost", Thread.currentThread().getName());
EventBus.getDefault().cancelEventDelivery(even);
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
注意 我在postingEven中 使用了 EventBus.getDefault().cancelEventDelivery(even);方法 目的 就是拦截了 其他优先级较低的订阅者的收发事件行为,只有该优先级或者优先级比此优先级相等的或者较高的才能够收到该事件;
如果我们将其取消 我们将会在onMainEventBus方法中运行 Textview会显示hello;
5.如果我们将EventBus.getDefault().cancelEventDelivery(even);注销掉
01-11 22:14:49.004 24534-24534/com.yaya.eventbusdemo E/===onPost: main
01-11 22:14:49.004 24534-21239/com.yaya.eventbusdemo E/===onAsy: pool-1-thread-1
01-11 22:14:49.004 24534-24534/com.yaya.eventbusdemo E/===Main: main
01-11 22:14:49.004 24534-21240/com.yaya.eventbusdemo E/===OnBack: pool-1-thread-2
显而易见的 优先级高的OnPostEven方法先运行 而且发现OnAsy和OnBack都在子线程中,
ThreadMode总共四个:
MAIN UI主线程
POSTING 默认调用方式,在调用post方法的线程执行,避免了线程切换,性能开销最少
BACKGROUND 如果调用post方法的线程不是主线程,则直接在该线程执行。
如果是主线程,则切换到后台单例线程,多个方法公用同个后台线程,按顺序执行,避免耗时操作
ASYNC 开辟新独立线程,用来执行耗时操作,例如网络访问。
6.如果我们将EventBus.getDefault().cancelEventDelivery(even);加上呢
01-11 22:14:49.004 24534-24534/com.yaya.eventbusdemo E/===onPost: main
01-11 22:14:49.004 24534-21239/com.yaya.eventbusdemo E/===onAsy: pool-1-thread-1
01-11 22:14:49.004 24534-24534/com.yaya.eventbusdemo E/===Main: main
01-11 22:14:49.004 24534-21240/com.yaya.eventbusdemo E/===OnBack: pool-1-thread-2
01-11 22:18:31.614 24534-24534/com.yaya.eventbusdemo E/===onPost: main
只运行onPost,根据ThreadMode特性其在当前发送线程主线程中;