We assume that such a business scenario is now running a chat application in a timely manner. We send and receive information on the chat page, and also need to update the chat records on the previous page in real time, how can we achieve this? Let's talk about my previous implementation strategy. I use the broadcast receiver broadcastreceiver, which keeps sending broadcasts when receiving and sending messages, and then receives updates where real-time updates are needed. The idea of implementation is relatively simple, and there is no code coupling problem, but there is a drawback. The disadvantage is that you need to implement broadcastrecevier in many places. Although the code is not redundant, it seems uncomfortable to have a lot of code.
The Otto introduced today can solve the problem of code size completely. Otto is a popular event bus framework designed to decouple applications while ensuring efficient communication between pages and modules. Otto is easy to use. The source code contains only nine classes, thousands of lines of code, and open APIs are indirectly clear. Otto is a design mode based on subscribe/publish. In short, if you want to subscribe to a message, you can use the @ subcribe annotation to receive the message.
Bus. Post (Object OBJ) is used to publish messages. This design achieves full decoupling.
The following steps describe the procedure.
1. Bus instantiation
The bus class is the soul of the entire framework. It is responsible for publishing and receiving messages. The whole process is implemented through this bus. We recommend that you use a singleton for bus instantiation. That is to say, only one bus object is instantiated in the entire application. All message processing is implemented through this single instance. Because the receiver of the message must process the same bus object to receive the published message. The bus constructor can receive parameters of the threadenforcer type. threadenforcer is actually an interface and has two implementations, indicating whether the bus runs in the main thread or asynchronous thread.
2. Register and unbind a bus
You can register and unbind a bus according to your specific business needs. For Android components, they are generally implemented based on the lifecycle method, and can be used in any custom class. The following shows the implementation in activity and fragment.
3. Message Publishing
Publishing a message is the most important part of the framework. It allows you to tell all subscribers that an event has been triggered. Instance objects of any class can be published through bus, and can only be received by recipients of subscribed objects. The following shows how to publish a message through bus. The message content is locationchangeevent, so the receiver of locationchangeevent can receive the published message.Note that only one object can be published.
4. Message subscription
Before subscribing to and publishing a message, you must register the bus in the current class. Subscription is a supplement to message Publishing. When a message publishing event is called, the corresponding message subscriber can receive the message immediately. The subscription function is implemented through a custom method. The method name can be customized and three conditions must be met.
1. Use the @ subscribe annotation BEFORE THE METHOD
2. The access modifier is public.
3. Set a single parameter based on the message you want to subscribe
Note: before use, remember to register; after use, remember to release.
5. Message produce
When the subscriber has registered, it usually needs to obtain the currently known value for a specific message. At this time, we need to useProduce.Same useProduceThe method name can be customized, and there are three points to note at the same time.
1. Use @ BEFORE THE METHOD @ProduceAnnotation
2. The access modifier is public.
3. No parameter. The return value is based on the subscriber parameter type.
Okay, that's what Otto uses. Refactor the code to get rid of endless broadcasts!
Of course, Otto has some disadvantages. To implement the above subscription/release model function, the price is to reflect the classes of each registered bus. If a large number of applications are used, the performance of the application may be affected.
Otto source and demo address: https://github.com/square/otto
If it is helpful to you, you are welcome to subscribe to my public account --Android Dry Goods sharing (ID: android_share). The QR code below provides you with timely and high quality Android dry goods.
Technical Exchange QQ group: 318588906. You are welcome to join the group to discuss Android and Java technologies and expand our sharing community.