关于EventBus的使用

本文详细介绍了如何在Android应用中使用EventBus进行事件传递,包括消息类的定义、Activity中的注册与注销、消息的发送与接收,以及不同线程环境下事件处理的策略。同时提供了关键jar包的下载链接。

使用步骤:

①:自己写个消息类(需要什么属性就添加什么字段)

eg: 

 public class MessageEvent {

    public final String message;


    public MessageEvent(String message) {

        this.message = message;

    }

}


②:在activity中注册和注销EventBus


EventBus.getDefault().register(this);


EventBus.getDefault().unregister(this);


③:在需要发送数据即消息的地方调用以下方法,发送你的消息类对象


EventBus.getDefault().post(new MessageEvent("fffffffffff"));


④:在需要接收数据即消息的地方,声明并实现“订阅函数”

@Subscribe ------------这个注解是订阅者的标记,不可省略

/**
     * MainThread: 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.
     *
     * @param messageEvent
     */
    @Subscribe
    public void onEventMainThread(MessageEvent messageEvent) {
        String msg = "onEventMainThread收到了消息:" + messageEvent.message;
        Log.d("harvic", msg);
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }

    /**
     * BackgroundThread: 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
     *
     * @param event
     */
    @Subscribe
    public void onEventBackgroundThread(MessageEvent event) {
        Log.d("harvic", "onEventBackground收到了消息:" + event.message);
    }

    /**
     * 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
     *
     * @param event
     */
    @Subscribe
    public void onEventAsync(MessageEvent event) {
        Log.d("harvic", "onEventAsync收到了消息:" + event.message);
    }

    /**
     * PostThread: 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 should return quickly to avoid blocking the posting thread, which
     * may be the main thread. Example:
     *
     * @param messageEvent
     */
    @Subscribe
    public void onEvent(MessageEvent event) {
        Log.d("harvic", "OnEvent收到了消息:" + event.message);
    }

所需jar包,可以自己打,这里我打包了一个,可以参考

http://pan.baidu.com/s/1c0mF5Tm




转载于:https://my.oschina.net/u/1539097/blog/520765

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值