android - Android 中的 greenrobot EventBus 发布事件

标签 android event-bus

通过使用 EventBus,我需要在一个 Activity 中发布一个事件 (MyEvent),并在 Android 中的另一个 Activity 中接收该事件。我尝试了 greenrobot EventBus 性能测试项目,但不知道如何做。

我在 ActivitySubscriber 中尝试过

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

并尝试在 ActivityReceiver 中接收事件

EventBus.getDefault().register(this);

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

但是我无法接收事件。谁能告诉我哪里做错了?

最佳答案

因为它们是两个 Activity ,ActivitySubscriber 发布事件,而 ActivityReceiver 仍未创建,或者处于停顿模式(onStop() ).您需要使用粘性事件,即

  • ActivitySubscriber.postSticky(...)

对于 ActivityReceiver,您有两个选择:

  • EventBus.getDefault().register(this) 之后的某处 EventBus.getDefault().getStickyEvent()
  • EventBus.getDefault().registerSticky() 然后使用常规 EventBus.getDefault().onEvent(...)

更新: EventBus 3.0 改变了订阅的方式。

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

如何使用版本 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 的更多详细信息和比较:

从源中提取的一些细节:

  • 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 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

    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

    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) 发布)传递给该订阅者(如果 Activity 可用)
    • priority = 0 - 影响事件传递顺序的订阅者优先级。在同一交付线程中,优先级较高的订阅者将先于其他优先级较低的订阅者接收事件。默认优先级为 0。注意:优先级不会影响具有不同线程模式的订阅者之间的传递顺序。

编辑2

现在有一个专门的网站来解答库创建者提出的任何 Greenrobot EventBus 问题:

http://greenrobot.org/eventbus/

关于android - Android 中的 greenrobot EventBus 发布事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14263183/

相关文章:

java - 从 Singleton 触发 GWT SimpleEventBus 事件时出现 Nullpointer [Umbrella]Exception

java - 如何将信息从一个 java 类传递到另一个

android - 如何创建已弃用版本的 Android 虚拟设备

android - Realm 自定义排序抛出 UnsupportedOperationException

android - Greenrobot Android Eventbus - 没有选项 eventbusindex 传递给注释处理器

android - 事件总线粘性事件

android - 使用 Otto 或 EventBus 之类的事件库是处理 Activity 、 fragment 和后台线程之间关系的推荐方法

java - 将 Activity 扩展到 AppCompatActivity

java - 如何添加mcxiaoke.volley:library

Android - 使用事件总线(如 Otto)进行 UI 元素通信是否正确?