EventBus3.0最新使用文档详解

EventBus

是一个发布/订阅事件总线用来优化android。

下面是一张经典的图片:

EventBus3.0最新使用文档详解_第1张图片

简化了组件之间的通信

      事件发送方和接受方

      使activity,fragment,后台线程更好的执行

      避免了复杂的依赖性和生命周期问题

使你的代码更加简单

性能更好

代码量小

用户量广泛

拥有先进的功能,如交互进程,用户优先级


首先在build.grade 文件中添加:

compile 'org.greenrobot:eventbus:3.0.0'

    1.创建一个对象事件,用于封装消息

  public class MessageEvent {
    public final String message;

    public MessageEvent(String message) {
        this.message = message;
    }
}

    2.设置订阅者,用于获取事件传递过来的消息

 

ThreadMode: POSTING(发送线程执行。。。)

Subscribers will be called in the same thread posting the event. This is the default. Event delivery is done synchronously and all subscribers will have been called once the posting is done. This ThreadMode implies the least overhead because it avoids thread switching completely. Thus this is the recommended mode for simple tasks that are known to complete is a very short time without requiring the main thread. Event handlers using this mode should return quickly to avoid blocking the posting thread, which may be the main thread. Example:
// Called in the same thread (default)

@Subscribe (threadMode  = ThreadMode. POSTING )  // ThreadMode is optional here
public  void onMessage (MessageEvent event )  {
    log (event. message ) ;
}

ThreadMode: MAIN(主线程执行)

Subscribers will be called in Android’s main thread (sometimes referred to as UI thread). If the posting thread is the main thread, event handler methods will be called directly (synchronously like described for ThreadMode.POSTING). Event handlers using this mode must return quickly to avoid blocking the main thread. Example:

// Called in Android UI's main thread
@Subscribe (threadMode  = ThreadMode. MAIN )
public  void onMessage (MessageEvent event )  {
    textField. setText (event. message ) ;
}

ThreadMode: BACKGROUND(后台线程执行)

Subscribers will be called in a background thread. If posting thread is not the main thread, event handler methods will be called directly in the posting thread. If the posting thread is the main thread, EventBus uses a single background thread that will deliver all its events sequentially. Event handlers using this mode should try to return quickly to avoid blocking the background thread.

// Called in the background thread
@Subscribe (threadMode  = ThreadMode. BACKGROUND )
public  void onMessage (MessageEvent event ) {
    saveToDisk (event. message ) ;
}

ThreadMode: ASYNC(强制在后台线程执行)

Event handler methods are called in a separate thread. This is always independent from the posting thread and the main thread. Posting events never wait for event handler methods using this mode. Event handler methods should use this mode if their execution might take some time, e.g. for network access. Avoid triggering a large number of long running asynchronous handler methods at the same time to limit the number of concurrent threads. EventBus uses a thread pool to efficiently reuse threads from completed asynchronous event handler notifications.

// Called in a separate thread
@Subscribe (threadMode  = ThreadMode. ASYNC )
public  void onMessage (MessageEvent event ) {
    backend. send (event. message ) ;
}


    3.注册和反注册


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

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

4. 发送事件

EventBus.getDefault().post(new MessageEvent("Hello everyone!"));




你可能感兴趣的:(android)