EventBus3.0最新使用文档详解

EventBus

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

下面是一张经典的图片:

简化了组件之间的通信

      事件发送方和接受方

      使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!"));




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值