Android Message Communication Third party open source project Eventbus usage

Source: Internet
Author: User

Eventbus is a third-party development library on GitHub, its project on GitHub home address: Https://github.com/greenrobot/EventBus
The Eventbus message model is the message Publisher/subscriber mechanism.

(1) Eventbus is the message publisher (send message)/Subscriber (Receive message) mode. Eventbus's message release is flexible enough to send messages anywhere in the project code, eventbus a single line of code to publish the message: Eventbus.getdefault (). Post (event); An event is an instance of a class that you define.

(2) Eventbus is initialized in the activity (or fragment) that receives the message. It is usually registered in the OnCreate of Android activity (or fragment) and requires only one line of code: Eventbus.getdefault (). Register (this); Similar to registering a message listening listener, do not forget to unregister Eventbus, in Ondestory inside Eventbus.getdefault (). Unregister (this);

(3) Eventbus receive messages. Write a Eventbus message receive function in activity according to the actual code: public void Oneventmainthread (MyEvent event); Then, as soon as Eventbus sends a message, it can be received here.

Eventbus message callback Receive Message function There are several: Oneventmainthread:main thread, this is closely related to the Android UI thread, do not block it! Oneventbackgroundthread: Hence the name Incredibles, the background thread receives processing. Oneventasync: Receives processing in an asynchronous thread.

1  PackageCom.lixu.chuandi;2 3 Importandroid.app.Activity;4 Importandroid.content.Intent;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.View;7 ImportAndroid.view.View.OnClickListener;8 ImportAndroid.widget.Button;9 ImportAndroid.widget.TextView;Ten ImportDe.greenrobot.event.EventBus; One  A  Public classMainactivityextendsActivity { - TextView TV; -  the @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); +TV =(TextView) Findviewbyid (R.ID.TV1); -Button Button =(Button) Findviewbyid (R.ID.BTN1); +Eventbus.getdefault (). Register ( This); AButton.setonclicklistener (NewOnclicklistener () { at  - @Override -              Public voidOnClick (View v) { -Event event =NewEvent (); -EVENT.A = "Hello, I am:"; -event.b = "small New"; in  - Eventbus.getdefault (). Post (event); toIntent Intent =NewIntent (mainactivity. This, Myappservice.class); + StartService (intent); -  the             } *         }); $ Panax Notoginseng     } -  the     //here, the Eventbus callback accepts the message and then updates the TextView of the main UI +      Public voidOneventmainthread (Event event) { A  the Tv.settext (event.tostring ()); +     } -  $ @Override $     protected voidOnDestroy () { -  -         Super. OnDestroy (); theEventbus.getdefault (). Unregister ( This); -Intent Intent =NewIntent (mainactivity. This, Myappservice.class);Wuyi StopService (intent); the     } -}
1  PackageCom.lixu.chuandi;2 3 ImportAndroid.app.Service;4 Importandroid.content.Intent;5 ImportAndroid.os.IBinder;6 ImportAndroid.util.Log;7 ImportDe.greenrobot.event.EventBus;8 9  Public classMyappserviceextendsService {Ten @Override One      Public voidonCreate () { A         Super. OnCreate (); -Eventbus.getdefault (). Register ( This); -     } the  - @Override -      Public intOnstartcommand (Intent Intent,intFlagsintStartid) { -Event event =NewEvent (); +         //be free to send messages to recipients at any point in the project - Eventbus.getdefault (). Post (event.tostring ()); +         return Super. Onstartcommand (Intent, flags, startid); A     } at  - @Override -      Publicibinder onbind (Intent Intent) { -         return NULL; -     } -  in     //receiving messages in the background -      Public voidOneventbackgroundthread (Event event) { to  +LOG.E ("Myappservice received the message:", event.tostring ()); -     } the}
1  PackageCom.lixu.chuandi;2 3  Public classEvent {4      PublicString A;5      PublicString B;6 @Override7      PublicString toString () {8         returnA +b;9     }Ten      One}

Android Message Communication Third party open source project Eventbus usage

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.