Android中的greenrobot EventBus发布事件 [英] greenrobot EventBus post event in Android

查看:384
本文介绍了Android中的greenrobot EventBus发布事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过使用EventBus,我需要在一个Activity中发布一个事件(MyEvent),并在Android中的另一个Activity中接收该事件.我尝试了greenrobot EventBus性能测试项目,但无法获取方法.

By using EventBus, I need to post an event(MyEvent) in an Activity and receive the event in another Activity in Android. I tried the greenrobot EventBus performance test project but could not get how to do it.

我在 ActivitySubscriber

MyEvent event = new MyEvent();
EventBus.getDefault().post(event);

并尝试以

EventBus.getDefault().register(this);

public void onEvent(MyEvent event){
....
}

但是我无法接收该事件.谁能告诉我我在哪里做错了吗?

but I am unable to receive the event. Can anyone let me know where am I doing wrong?

推荐答案

由于这是两个活动,因此 ActivitySubscriber 会发布事件,而 ActivityReceiver 仍未创建,或者是在停止模式下( onStop()).您需要使用粘性事件,即

Since they are two activities, ActivitySubscriber posts the event while ActivityReceiver is still not created, or is in stall mode (onStop()). You need to use sticky events, i.e.

  • ActivitySubscriber.postSticky(...)

对于ActivityReceiver,您有两个选择:

And for ActivityReceiver you have two options:

  • EventBus.getDefault().register(this)及其后的某个地方 EventBus.getDefault().getStickyEvent()
  • EventBus.getDefault().registerSticky(),然后使用常规的 EventBus.getDefault().onEvent(...)
  • EventBus.getDefault().register(this) and somewhere after that EventBus.getDefault().getStickyEvent()
  • EventBus.getDefault().registerSticky() and then using regular EventBus.getDefault().onEvent(...)

已更新:EventBus 3.0更改了订阅方式.

Updated: EventBus 3.0 changes the way to subscribe.

不需要以特定后缀而是注释结尾的方法名称.

There is no need of method names which end up with specific suffixes but rather annotations.

如何使用版本3:

//// in your build.gradle
compile 'de.greenrobot:eventbus:3.0.0-beta1'
// alternatively you can target latest whatever currently
// compile 'de.greenrobot:eventbus:+'

//// from a class which needs to dispatch an event
// posting an event is as before, no changes
// here we dispatch a sticky event
EventBus.getDefault().postSticky(myStickyEvent);

//// from your class which needs to listen
// method name can be any name
// any of subscribe params is optional, i.e. can use just @Subscribe
@Subscribe(threadMode = ThreadMode.MainThread, sticky = true, priority = 1)
public void onEventBusEvent(@Nullable final StickyEvent stickyEvent) {
    if (stickyEvent != null) {
      ...
      // optionally you can clean your sticky event in different ways
      EventBus.getDefault().removeAllStickyEvents();
      // EventBus.getDefault().removeStickyEvent(stickyEvent);
      // EventBus.getDefault().removeStickyEvent(StickyEvent.class);
    }
}

有关版本3的更多详细信息和比较,

For more details and comparison of version 3:

从来源中提取的一些详细信息:

Some details extracted from the sources:

  • ThreadMode.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 must return quickly to avoid blocking the posting thread, which may be the main thread.

  • ThreadMode.MainThread

    Subscriber将在Android的主线程(有时称为UI线程)中调用.如果发布线程是主线程,则将直接调用事件处理程序方法.使用此模式的事件处理程序必须快速返回以避免阻塞主线程.

    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.

  • ThreadMode.BackgroundThread

    订户将在后台线程中被调用.如果发布线程不是主线程,则将在发布线程中直接调用事件处理程序方法.如果发布线程是主线程,则EventBus使用单个后台线程,该线程将按顺序传递其所有事件.使用此模式的事件处理程序应尝试快速返回以避免阻塞后台线程.

    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.

  • ThreadMode.Async

    事件处理程序方法在单独的线程中调用.这始终独立于发布线程和主线程.发布事件永远不会等待使用此模式的事件处理程序方法.如果事件处理程序方法的执行可能需要一些时间,例如用于网络访问.避免同时触发大量长时间运行的异步处理程序方法,以限制并发线程的数量.EventBus使用线程池来有效地重用已完成的异步事件处理程序通知中的线程.

    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.

  • @Subscribe
  • 默认
    • threadMode = ThreadMode.PostThread
    • sticky = false -如果为true,则向该订阅者传递最新的粘性事件(带有 de.greenrobot.event.EventBus.postSticky(Object)发布)活动可用)
    • priority = 0 -影响事件传递顺序的订户优先级.在同一个传递线程中,优先级较高的订户将比其他优先级较低的订户接收事件.默认优先级为0.注意:优先级 NOT 不会影响具有不同线程模式的订户之间的传递顺序.
    • default values for @Subscribe
      • threadMode = ThreadMode.PostThread
      • sticky = false - If true, delivers the most recent sticky event (posted with de.greenrobot.event.EventBus.postSticky(Object) to this subscriber (if event available)
      • priority = 0 - Subscriber priority to influence the order of event delivery. Within the same delivery thread, higher priority subscribers will receive events before others with a lower priority. The default priority is 0. Note: the priority does NOT affect the order of delivery among subscribers with different thread modes.
      • 现在有一个专门的站点,可以解决lib的创建者提出的有关Greenrobot EventBus的任何问题:

        There is a dedicated site now for any Greenrobot EventBus questions from the creator of the lib:

        http://greenrobot.org/eventbus/

        这篇关于Android中的greenrobot EventBus发布事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆