What's the difference between Broadcastreceiver and Eventbus?

Source: Internet
Author: User

What is the difference between broadcastreceiver and eventbus? They are alike, when to use Broadcastreceiver, when to use Eventbus?

Android broadcasts are divided into two areas: the broadcast sender and the broadcast receiver, usually broadcastreceiver refers to the broadcast recipient (broadcast receiver).

Eventbus is a publish/Subscribe event bus. To put it simply, the two people agreed how to communicate, one to publish the news, another agreed to the good person immediately received your message.

Use: I believe everyone has used handle. Thread communication, callback method to communicate. Eventbus can help reduce a lot of things, regardless of where you post an event anywhere, the recipient can immediately receive your message without you having to consider the problem of the Android sub-thread manipulating the UI thread.

First, the broadcast as a means of communication between Android components, you can use the following scenarios:

1, the same app inside the same component within the message communication (between single or multiple threads);

2. Message communication between different components within the same app (single process);

3, the same app has multiple processes of the different components of message communication;

4. Message communication between components in different apps;

5, the Android system in certain circumstances and the message between the app communication.

Second, the above enumerated broadcasting mechanism can be used in the scene, in the practical application of applicability:

1, the same app within the same component of the message communication (between single or multiple threads), the actual application will certainly not use the broadcast mechanism (although can be used), whether it is the use of extended variable scope, interface-based callback or handler-post/handler-message, etc. , can directly deal with such problems, if the application of broadcasting mechanism, obviously some "kill chicken sledgehammer" feeling;

2, the same app within the different components of the message communication (a single process), for such requirements, in some teaching complex circumstances simply rely on interface-based callback and other ways bad processing, at this time can directly use Eventbus, relatively speaking, eventbus because of the unified process, It is ideal for dealing with such requirements and is easily decoupled.

3, other circumstances, because of the communication between different processes, according to the actual business use of broadcasting mechanism will be very appropriate.

Third, the concrete implementation of the Broadcastreceiver process is as follows:

1, the broadcast recipient broadcastreceiver through the binder mechanism to the AMS (Activity Manager Service) registration;

2, the broadcast sender through the binder mechanism to send the broadcast to AMS;

3, AMS to find the corresponding conditions (intentfilter/permission, etc.) of the Broadcastreceiver, the broadcast sent to Broadcastreceiver (in general, activity) corresponding message loop queue;

4, message loop execution get this broadcast, callback Broadcastreceiver in the OnReceive () method.

Iv. use of the Eventbus framework specific procedures are as follows:

1. Register Eventbus.getdefault () when initializing. Register (this);

2. After use, write off Eventbus.getdefault (). Unregister (this);

3, the middle process is mainly the message push and receive, through the Eventbus.getdefault (). Post (param) push via oneventmainthread (param), oneventpostthread (param), Oneventbackgroundthread (param), oneventasync (param) receives and processes.

Thus, the broadcast sender and the broadcast receiver belong to both ends of the message publication and subscription in The Observer pattern, and AMS belongs to the middle processing center. The execution of the broadcast sender and the broadcast receiver is asynchronous, and the outgoing broadcast does not care whether or not the recipient receives it, or when the recipient is actually receiving it. Clearly, the overall process is very similar to Eventbus.

What's the difference between Broadcastreceiver and Eventbus?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.