android EventBus 使用

ThreadMode:POSTING
默认类型.将会在发送事件的线程执行,当事件发送完成,将同步交付给所有监听该事件的监听者,推荐用于那些很短时间完成且不需要在主线程运行的任务,事件处理必须尽快完成,否则可能会阻塞事件发送线程(有可能是主线程),将会导致事件分发延迟。

ThreadMode:MAIN
将会在主线程执行,如果本身是在主线程发送的事件,事件处理方法将会直接调用它。时间处理必须尽快完成,避免阻塞主线程

ThreadMode:BACKGROUND
将会在后台线程执行,如果事件不是在主线程发送的,将会直接在发送它的线程调用它,如果是在主线程发送,EventBus 将会将它们在一个单独的后台线程按次序处理,事件处理应当尽快完成,以免阻塞后台线程.


ThreadMode:ASYNC
事件处理方法将会在一个单独的线程调用,它总是与发送它的线程和主线程不同,发送线程永远不会因为它的处理而等待,如果事件处理需要大量时间可以使用该模式,避免同一时间有大量异步的事件处理线程执行,EventBus 使用线程池来完成任务.

StickyEvent
总是会保留最后一次发送的事件

定义事件

package example.com.eventbusdemo;

public class Event {

    private  String msg;

    Event(String msg){
        this.msg=msg;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg =msg;
    }
}

注册,发送,接收事件


public class MainActivity extends AppCompatActivity {

    public static String TAG="EVENTBUSLOG";

    EventBus bus=EventBus.getDefault();

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

        Log.d(TAG, "主线程:" + Thread.currentThread().getId());
        String msg="创建:"+Thread.currentThread().getId()+"MainThread";
        Event event=new Event(msg);
        bus.post(event);

        new WorkThread().start();


    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Event event){
        Log.d(TAG, "MAIN 运行:" +Thread.currentThread().getId()+ event.getMsg());
    }

    @Subscribe(threadMode = ThreadMode.BACKGROUND,sticky = true)
    public void onBackThread(Event event){
        Log.d(TAG, "BACK 运行:"+Thread.currentThread().getId()+event.getMsg());
    }

    @Subscribe(threadMode = ThreadMode.ASYNC)
    public void onAsync(Event event){
        Log.d(TAG, "ASYNC 运行:" + Thread.currentThread().getId() + event.getMsg());
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onPostiong(Event event){
        Log.d(TAG, "POSTING 运行:"+Thread.currentThread().getId()+event.getMsg());
    }


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

    class  WorkThread extends  Thread
    {
        @Override
        public void run() {
              String msg="创建:"+getId()+"WorkThread";
              Event event=new Event(msg);
              bus.post(event);
          }
    }

}

结果

02-19 07:30:24.799 2139-2139/example.com.eventbusdemo D/EVENTBUSLOG﹕ 主线程:1
02-19 07:30:24.800 2139-2139/example.com.eventbusdemo D/EVENTBUSLOG﹕ MAIN 运行:1创建:1MainThread
02-19 07:30:24.800 2139-2139/example.com.eventbusdemo D/EVENTBUSLOG﹕ POSTING 运行:1创建:1MainThread
02-19 07:30:24.806 2139-2155/example.com.eventbusdemo D/EVENTBUSLOG﹕ BACK 运行:113创建:113WorkThread
02-19 07:30:24.806 2139-2155/example.com.eventbusdemo D/EVENTBUSLOG﹕ POSTING 运行:113创建:113WorkThread
02-19 07:30:24.808 2139-2154/example.com.eventbusdemo D/EVENTBUSLOG﹕ BACK 运行:112创建:1MainThread
02-19 07:30:24.815 2139-2157/example.com.eventbusdemo D/EVENTBUSLOG﹕ ASYNC 运行:114创建:1MainThread
02-19 07:30:24.816 2139-2153/example.com.eventbusdemo D/EVENTBUSLOG﹕ ASYNC 运行:111创建:113WorkThread
02-19 07:30:24.818 2139-2139/example.com.eventbusdemo D/EVENTBUSLOG﹕ MAIN 运行:1创建:113WorkThread

StickyEvent


    Event stickyEvent=new Event("sticky 1");
    Event stickyEvent2=new Event("sticky 2");
    bus.postSticky(stickyEvent);
    bus.postSticky(stickyEvent2);

    Button button=(Button)findViewById(R.id.send_event);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
            startActivity(intent);
        }
    });

AnotherActivity

public class AnotherActivity extends AppCompatActivity {


    public static  String TAG="AnotherTag";

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

    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Subscribe(sticky = true)
    public void onEvent(Event event){
        Log.d(TAG, event.getMsg()) ;
    }

    @Subscribe
    void onMainEvent(Event event){
        Log.d(TAG, event.getMsg()) ;
    }


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

运行结果

02-19 07:34:38.547 4206-4206/example.com.eventbusdemo D/EVENTBUSLOG﹕ 主线程:1
02-19 07:34:38.547 4206-4222/example.com.eventbusdemo D/EVENTBUSLOG﹕ ASYNC 运行:167创建:1MainThread
02-19 07:34:38.547 4206-4223/example.com.eventbusdemo D/EVENTBUSLOG﹕ BACK 运行:168创建:1MainThread
02-19 07:34:38.547 4206-4206/example.com.eventbusdemo D/EVENTBUSLOG﹕ MAIN 运行:1创建:1MainThread
02-19 07:34:38.547 4206-4206/example.com.eventbusdemo D/EVENTBUSLOG﹕ POSTING 运行:1创建:1MainThread
02-19 07:34:38.548 4206-4222/example.com.eventbusdemo D/EVENTBUSLOG﹕ ASYNC 运行:167sticky 1
02-19 07:34:38.548 4206-4223/example.com.eventbusdemo D/EVENTBUSLOG﹕ BACK 运行:168sticky 1
02-19 07:34:38.548 4206-4206/example.com.eventbusdemo D/EVENTBUSLOG﹕ MAIN 运行:1sticky 1
02-19 07:34:38.548 4206-4206/example.com.eventbusdemo D/EVENTBUSLOG﹕ POSTING 运行:1sticky 1
02-19 07:34:38.548 4206-4222/example.com.eventbusdemo D/EVENTBUSLOG﹕ ASYNC 运行:167sticky 2
02-19 07:34:38.548 4206-4223/example.com.eventbusdemo D/EVENTBUSLOG﹕ BACK 运行:168sticky 2
02-19 07:34:38.548 4206-4206/example.com.eventbusdemo D/EVENTBUSLOG﹕ MAIN 运行:1sticky 2
02-19 07:34:38.548 4206-4206/example.com.eventbusdemo D/EVENTBUSLOG﹕ POSTING 运行:1sticky 2
02-19 07:34:38.550 4206-4222/example.com.eventbusdemo D/EVENTBUSLOG﹕ ASYNC 运行:167创建:175WorkThread
02-19 07:34:38.550 4206-4703/example.com.eventbusdemo D/EVENTBUSLOG﹕ BACK 运行:175创建:175WorkThread
02-19 07:34:38.550 4206-4703/example.com.eventbusdemo D/EVENTBUSLOG﹕ POSTING 运行:175创建:175WorkThread
02-19 07:34:38.641 4206-4206/example.com.eventbusdemo D/EVENTBUSLOG﹕ MAIN 运行:1创建:175WorkThread

可以看出,发送 stickyEvent 时,没有指定 sticky=true 的接收方法也可以接收,在开启另一个 Activity 前发送了两个 StickyEvent ,而在那 Activity 中只有指定了 sticky 的接收到了事件,且是之前最后发送的事件

你可能感兴趣的:(android)