通过使用 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 的更多详细信息和比较:
- http://androiddevblog.com/eventbus-3-droidcon/
- http://androiddevblog.com/wordpress/wp-content/uploads/EventBus3.pdf
从源中提取的一些细节:
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.
- 默认值
threadMode = ThreadMode.PostThread
sticky = false
- 如果为 true,则将最近的粘性事件(使用de.greenrobot.event.EventBus.postSticky(Object)
发布)传递给该订阅者(如果 Activity 可用)priority = 0
- 影响事件传递顺序的订阅者优先级。在同一交付线程中,优先级较高的订阅者将先于其他优先级较低的订阅者接收事件。默认优先级为 0。注意:优先级不会影响具有不同线程模式的订阅者之间的传递顺序。
@Subscribe
的编辑2
现在有一个专门的网站来解答库创建者提出的任何 Greenrobot EventBus 问题:
关于android - Android 中的 greenrobot EventBus 发布事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14263183/