Communication Within an Android App With EventBus
A typical Android app tends to be composed of many layers, modules or structures such as Fragments, Activities, Presenters, and Services. Effective communication between these components can become difficult if they are tightly coupled together. 典型的Android应用程序通常由许多层,模块或结构组成,例如Fragments,Activities,Presenters和Services。如果这些部件紧密耦合在一起,这些部件之间的有效通信可能变得困难。
In the lower level of your app architecture, such as the database, when an action happens, you might want to send data to a higher level such as the view. To do this, you might want to create a listener interface, async tasks or callbacks. All of these will work, but they have some major drawbacks:在应用程序架构的较低级别(例如数据库)中,当发生操作时,您可能需要将数据发送到更高级别(例如视图)。为此,您可能需要创建侦听器接口,异步任务或回调。所有这些都将工作,但他们有一些主要的缺点:
- direct or tight coupling
- registering and unregistering multiple dependencies individually
- repetition of code
- difficulty in testing
- increased risk of bugs
直接或紧密耦合
单独注册和注销多个依赖项
重复代码
测试难度
增加错误的风险
Using publish/subscribe or message bus architecture prevents all the potential problems highlighted above. It is a very good way to implement effective communications between components in an application without any of them needing to be aware of the others immediately. Using publish/subscribe in Android, any app component can publish events which it will hand over to the bus, and the relevant consumers can consume or subscribe to them. 使用发布/订阅或消息总线架构可防止上面强调的所有潜在问题。这是在应用中的组件之间实现有效通信的非常好的方式,而不需要他们立即知道其他组件。在Android中使用发布/订阅,任何应用组件都可以发布它将传递到总线的事件,相关的消费者可以消费或订阅它们。
build.gradle file, include compile 'org.greenrobot:
eventbus
:3.0.0'
, and then sync your project afterwards. 要使用greenrobot EventBus,您需要首先将其添加到应用程序模块build.gradle文件中,包括compile’org.greenrobot:eventbus:3.0.0’,然后同步您的项目。
An Event Subscriber事件订阅者
A subscriber simply subscribes to an event by registering in the event bus and can also unregister that event. To be a subscriber, you have to do three main things:订户通过在事件总线中注册来简单地订阅事件,并且还可以注销该事件。要成为订阅者,你必须做三件事:
1. Register the subscriber in the event bus with register()
. This informs the event bus that you want to begin receiving events. In an activity, this is in the onStart()
method, while in a fragment put this in theonAttact(Activity activity)
method. 1.使用register()注册事件总线中的用户。这通知事件总线您想要开始接收事件。在一个活动中,这是在onStart()方法,而在一个fragment 片段在onAttact(活动活动)方法。
1 2 3 4 5 |
|
2. Unregister the subscriber, which means tell the event bus to stop sending me events. In an activity, this is in the onStop()
method, while in a fragment put this in the onDetach()
method. 2.注销订阅者,这意味着告诉事件总线停止发送事件。在一个活动中,这是在onStop()方法,而在一个片段放在onDetach()方法。
1 2 3 4 5 |
3. Implement the onEvent()
to indicate the type of event you want to receive and action to take when you receive the event. Notice the @Subscribe
annotation at the top of this method. In this case, we want to subscribe to a normal event and not a sticky one—I’ll explain the difference later.3.实现onEvent()以指示您要接收的事件的类型,以及在接收事件时采取的操作。请注意此方法顶部的@Subscribe注释。在这种情况下,我们想订阅一个正常的事件,而不是一个粘滞的事情 – 我以后解释差异。
1 2 3 4 |
|
Defining Event Messages定义事件消息
The events in greenrobot EventBus are just objects that you define. You can have different event classes if you want. They do not inherit any base class or interface—they’re just POJO (Plain Old Java Objects). greenrobot EventBus中的事件只是您定义的对象。如果需要,您可以有不同的事件类。它们不继承任何基类或接口 – 它们只是POJO(普通Java对象)。
01 02 03 04 05 06 07 08 09 10 11 12 |
|
Post Event and Post Sticky Event发布事件和滞留事件
滞留事件时,此事件存储在缓存中。当新的活动或片段订阅事件总线时,它从高速缓存获取最新的滞留事件,而不是等待它再次被触发到事件总线 – 因此即使在订阅者获得它之后,该事件仍然保留在高速缓存中。
Sticky events are posted with the postSticky(MessageEvent)
method, and non-sticky events with thepost(MessageEvent)
method.滞留事件使用postSticky(MessageEvent)方法发布,非滞留事件使用postpost(MessageEvent)方法发布。
1 2 |
|
滞留事件,如果没有找到订户,则事件将被丢弃。然而,滞留事件将被缓存,以防用户后来出现。
滞留事件?如果您要跟踪用户的位置,或者简单缓存数据,跟踪电池电量等,则可以执行此操作。
1 |
|
Subscribe to Post Sticky Event
1 2 3 4 5 |
|
To subscribe to a sticky event, you include sticky = true
inside the @Subscribe
annotation. This indicates that we want to receive a sticky event of type MessageEvent
from the cache. 要订阅粘性事件,请在@Subscribe注释中包含sticky = true。这表明我们想从缓存中接收MessageEvent类型的sticky事件。
Removing Sticky Events删除粘性事件
1 2 3 4 |
|
removeStickyEvent(Event)
removes a sticky event from the cache, and removeAllStickyEvents()
will remove all sticky events. removeStickyEvent(Event)从缓存中删除一个粘性事件,removeAllStickyEvents()将删除所有粘性事件。
EventBus Thread Modes EventBus线程模式
There are four thread modes available for subscribers to choose from: posting, main, background, and async. 有四种线程模式可供订户选择:posting,main,background和async。
Posting
1 |
|
This is the default. Subscribers will be called in the same thread as the thread where the event is posted. Including ThreadMode.POSTING
in your @Subscribe
annotation is optional. 这是默认值。订阅者将在与事件发布的线程相同的线程中被调用。在@Subscribe注释中包含ThreadMode.POSTING是可选的。
Main
1 |
|
In this thread mode, subscribers will receive events in the main UI thread, no matter where the event was posted. This is the thread mode to use if you want to update UI elements as a result of the event.在此线程模式下,订阅者将在主UI线程中接收事件,无论事件发布在何处。这是如果你想更新UI元素作为事件的结果使用的线程模式。
Background
1 |
|
In this thread mode, subscribers will receive events in the same thread where they are posted, just like for ThreadMode.POSTING
. The difference is that if the event is posted in the main thread, then subscribers will instead get them on a background thread. This makes sure that event handling doesn’t block the app’s UI. Still, don’t run an operation that will take a long time on this thread. 在这个线程模式中,订阅者将在同一个线程中接收它们被发布的事件,就像ThreadMode.POSTING。不同的是,如果事件发布在主线程中,那么订阅者将在后台线程上获取它们。这确保事件处理不会阻止应用程序的UI。仍然,不要运行将需要很长时间在此线程上的操作。
Async
1 |
|
In this thread mode, subscribers will always receive events independently from the current thread and main thread. This enables the subscribers to run on a separate thread. This is useful for long-running operations such as network operations. 在此线程模式中,订阅者将始终接收独立于当前线程和主线程的事件。这使得订阅者能够在单独的线程上运行。这对于长时间运行的操作(如网络操作)很有用。
Subscriber Priorities 订阅者优先级
If you want to change the order in which subscribers get events, then you need to specify their priority levels during registration. Subscribers with a higher priority get the event before subscribers with a lower priority. This only affects subscribers in the same thread mode. Note that the default priority is 0. 如果要更改订户获得事件的顺序,则需要在注册期间指定其优先级。具有较高优先级的订户在具有较低优先级的订户之前获得该事件。这只会影响相同线程模式中的订阅者。请注意,默认优先级为0。
1 2 3 4 |
|
Cancelling Events取消事件
If you want to stop an event from being delivered to other subscribers, call the cancelEventDelivery(Object event)
method inside the subscriber’s event handling method. 如果要停止将事件传递给其他订户,请调用订户的事件处理方法中的cancelEventDelivery(Object event)方法。
1 2 3 4 |
|
Conclusion 结论
In this tutorial, you learned about:
- greenrobot EventBus and how it can improve your Android app
- the difference between regular and sticky events
- the different thread modes available and when to use each one
- subscriber priorities
- cancelling an event to stop receiving events
greenrobot EventBus以及如何改进您的Android应用程序
正常事件和粘性事件之间的差异
不同的线程模式可用和何时使用每一个
订户优先级
取消停止接收事件的事件
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
相关推荐: 《现代JavaScript高级教程》 JavaScript修饰器:简化代码,增强功能
点击在线阅读,体验更好 链接 现代JavaScript高级小册 链接 深入浅出Dart 链接 现代TypeScript高级小册 链接 JavaScript修饰器:简化代码,增强功能 引言 在JavaScript中,修饰器(Decorator)是一种特殊的语法,…