Eventbus and broadcast

Source: Internet
Author: User
Tags eventbus

What the hell is broadcastreceiver? Broadcasting in Android is divided into two areas: the broadcast sender and the broadcast receiver, usually broadcastreceiver refers to the broadcast recipient (broadcast receiver).

What the hell is Eventbus? 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. 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 scenario, 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 very suitable and easy to handle such requirements.

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 intermediate process is mainly the message push and receive, through Eventbus.getdefault (). Post (param) push via oneventmainthread (param), oneventpostthread (param), Oneventbackgroundthread (param), oneventasync (param) receives and processes.

    由此看来,广播发送者和广播接收者分别属于观察者模式中的消息发布和订阅两端,AMS属于中间的处理中心。广播发送者和广播接收者的执行是异步的,发出去的广播不会关心有无接收者接收,也不确定接收者到底是何时才能接收到。显然,整体流程与EventBus非常类似。

Eventbus and broadcast

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.