EventBus实战笔记

使用场景:

         在B界面里面做了某些操作更新A界面的页面,在后台服务里面获取到了数据更新前台页面。

1.添加依赖
compile 'org.greenrobot:eventbus:3.0.0'
2.在OnCreate中注册EventBus
Eventbus.getDefault.register(this);
3.在onDestory中取消注册
EventBus.getDefault().unregister(this);
4.处理订阅信息
定义一个方法,加上
@Subscribe(threadMode = ThreadMode.MAIN)标记
threadMode 有四个值,分别代表着不一样的含义:
ThreadMode.MAIN代表在主线程中处理
ThreadMode.POSTING 默认调用方式,在调用post方法的线程执行,避免了线程切换,性能开销最少
ThreadMode.ASYNC开辟新独立线程,用来执行耗时操作,例如网络访问

 

ThreadMode.BACKGROUND如果调用post方法的线程不是主线程,则直接在该线程执行,如果是主线程,则切换到后台单例线程,多个方法公用同个后台线程,按顺序执行,避免耗时操作

 

package org.greenrobot.eventbus;
/**
 * Each event handler method has a thread mode, which determines in which thread the method is to be called by EventBus.
 * EventBus takes care of threading independently from the posting thread.
 * 
 * @see EventBus#register(Object)
 * @author Markus
 */
public enum ThreadMode {
    /**
     * Subscriber will be called in the same thread, which is posting the event. This is the default. Event delivery
     * 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 must return quickly to avoid blocking the posting thread, which may be the main thread.
     */
    POSTING,


    /**
     * Subscriber 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. Event handlers using this mode must return
     * quickly to avoid blocking the main thread.
     */
    MAIN,


    /**
     * Subscriber 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.
     */
    BACKGROUND,


    /**
     * 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.
     */
    ASYNC
}

@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(Bean bean){
//接收到广播后处理数据,如刷新界面等操作
}
5.发送信息(在需要更新数据的地方发送广播)
EventBus.getDefault().post(new Bean());

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值