45. Use of the Android event bus distribution Library

Source: Internet
Author: User
Tags eventbus

Event Bus Distribution Library introduction and comparison of Eventbus and Otto

What is event Bus management:

A. Placing events in a queue for management and distribution
B. Ensure efficient communication and data and event distribution between the various parts of the application
C. Decoupling between modules

The event bus is a publish/subscribe events Fieldbus. Event Bus Mode-also known as message bus or publisher/Subscriber (Publisher/subscriber)
Mode-allows two of components to communicate with each other, but they do not know each other.
Based on the event bus management/subscription/distribution mode. The event response has more thread choices, and Eventbus can publish events to different threads.
Eventbus supports Sticky Event.
You need to register a subscription and then distribute the message data to subscribers. Contains 4 Ingredients: Publisher, subscriber, event, bus. Subscribers can subscribe to multiple events,
The sender can post any event, and the publisher can also be a subscriber. Sub-subscription, registration, release, cancellation and other steps.

Basic usage of Event bus

Subscribe, register, publish, and unregister.
Registered:
Eventbus.getdefault (). Register (this);
Eventbus.getdefault (). Register (new MyClass ());
Registration: Three parameters are, message subscribers (recipients), receive method name, event class
Eventbus.getdefault (). Register (This, "Settexta", Settextaevent.class);

Cancel Registration:
Eventbus.getdefault (). Unregister (this);
Eventbus.getdefault (). Unregister (new MyClass ());

Subscription processing data:
public void oneventmainthread{}
public void OnEvent (Anyeventtype event) {}
Oneventpostthread, Oneventbackgroundthread, Oneventasync

Release:
Eventbus.getdefault (). Poststicky (New Secondactivityevent ("Message from Secondactivity"));
Eventbus.getdefault (). Post (new Changeimgevent (1));

Eventbus Practical Project Application case

Achieve the effect: One click to send data, another place or multiple registration points can be timely access to update the data transmitted over.

knowledge points are involved: event class customization, registration, subscription events, event Publishing, data parsing, and unregister.

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >    <ButtonAndroid:id= "@+id/btn_send"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Send Event" />    <TextViewAndroid:id= "@+id/tv_content"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_margintop= "20DP"android:gravity= "Center"android:textsize= "20SP" /></LinearLayout>

/*** Eventbus Demonstration of actual project case*/ Public classMainactivityextendsActivity {PrivateTextView tv_content; PrivateButton Btn_send; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Tv_content= (TextView) This. Findviewbyid (r.id.tv_content); Btn_send= (Button) This. Findviewbyid (R.id.btn_send); Btn_send.setonclicklistener (NewOnclicklistener () {@Override//Sending data Events             Public voidOnClick (View arg0) {myevent my=NewMyEvent (); My.settype ("1"); My.setcontent ("1 Content");            Eventbus.getdefault (). post (my);        }        }); Eventbus.getdefault (). Register ( This); }     Public voidonEvent (MyEvent event) {if(Event.gettype (). Equals ("0") {tv_content.settext (event.getcontent ()); }    }     Public voidOneventmainthread (MyEvent event) {if(Event.gettype (). Equals ("1") {tv_content.settext (event.getcontent ()); }} @Overrideprotected voidOnDestroy () {Super. OnDestroy (); Eventbus.getdefault (). Unregister ( This); }}

 Public classMyEvent {PrivateString type; PrivateString content;  PublicString GetType () {returntype; }     Public voidsetType (String type) { This. Type =type; }     PublicString getcontent () {returncontent; }     Public voidsetcontent (String content) { This. Content =content; }}

45. Use of the Android event bus distribution Library

Related Article

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.