Eventbus is the most recent project to use, but also just a few simple features, but the feeling is quite powerful. Code Link: http://download.csdn.net/detail/qq_29774291/9629346
Eventbus is the guava event handling mechanism, an elegant implementation of the Observer pattern (production/consumer programming model) in design patterns. The main function is to substitute intent,handler,broadcast for passing messages between Fragment,activity,service and threads. The advantage is that the cost is small and the code is more elegant. and decoupling the sender and receiver.
A. Downloaded class library: Https://github.com/greenrobot/EventBus
Two. Basic use:
1. Register on the page where you want to receive the message:
1 eventbus.getdefault (). Register (this);
2. Send a message in the Send interface:
New Student (20, "Zhang San", "male"); Eventbus.getdefault (). Post (student);
3. Accept the information on the acceptance screen:
@Subscribe Public void onmessagereviced (final Student Message) { // display a pop-up window System.out.println ("Received message DDDX:" + message ); if NULL { Showalertdialog (This, "name:" + message.getname () + "\ \ Age:" + message.getage () + "\ \ Gender:" +<
C17>message . Getsex ()); } }
4. Cancellation of registration:
@Override protectedvoid OnDestroy () { // TODO auto-generated method Stub Super. OnDestroy (); Eventbus.getdefault (). Unregister (this); }
Main program code
1 Packagecom.item.jiejie.event;2 3 ImportDe.greenrobot.event.EventBus;4 ImportDe.greenrobot.event.Subscribe;5 ImportAndroid.os.Bundle;6 Importandroid.app.Activity;7 ImportAndroid.app.AlertDialog;8 ImportAndroid.content.Context;9 ImportAndroid.content.DialogInterface;Ten Importandroid.content.Intent; One ImportAndroid.view.View; A ImportAndroid.widget.Button; - ImportAndroid.widget.TextView; - the Public classMainactivityextendsActivity { - - PrivateTextView Tv_view; - PrivateButton btn_to_finish; + - @Override + protected voidonCreate (Bundle savedinstancestate) { A Super. OnCreate (savedinstancestate); at Setcontentview (r.layout.activity_main); - Setview (); -Eventbus.getdefault (). Register ( This); - } - - @Override in protected voidOnDestroy () { - //TODO auto-generated Method Stub to Super. OnDestroy (); +Eventbus.getdefault (). Unregister ( This); - } the Private voidSetview () { * //TODO auto-generated Method Stub $Tv_view =(TextView) Findviewbyid (r.id.tv_text);Panax NotoginsengBtn_to_finish =(Button) Findviewbyid (r.id.btn_to_one); -Btn_to_finish.setonclicklistener (NewView.onclicklistener () { the + @Override A Public voidOnClick (View arg0) { the //TODO auto-generated Method Stub +StartActivity (NewIntent (mainactivity. This, Oneactivity.class)); - } $ }); $ } - Private BooleanIsshow =false; - @Override the protected voidOnStart () { - //TODO auto-generated Method StubWuyi Super. OnStart (); theIsshow =true; - } Wu @Override - protected voidOnStop () { About //TODO auto-generated Method Stub $ Super. OnStop (); -Isshow =false; - } - @Subscribe A Public voidOnmessagereviced (FinalString Message) { + Tv_view.settext (Message); theSystem.out.println ("Received message DDD:" +Message); - } $ the @Subscribe the Public voidOnmessagereviced (FinalStudent Message) { the //show a pop -up window theSystem.out.println ("Received message DDDX:" +Message); - if(Message! =NULL){ inShowalertdialog ( This, "Name:" + message.getname () + "\ \ Age:" + message.getage () + "\ \ Sex:" +Message the . Getsex ()); the } About } the Private voidShowalertdialog (FinalContext context,string String) { theAlertdialog.builder dialog =NewAlertdialog.builder (context); theDialog.settitle ("I am the title"); + dialog.setmessage (string); -Dialog.setpositivebutton ("OK",NewDialoginterface.onclicklistener () { the Bayi @Override the Public voidOnClick (Dialoginterface arg0,intarg1) { the //TODO auto-generated Method Stub - - } the }); theAlertdialog Mdialog =dialog.create (); the mdialog.show (); the } -}
Simple implementation of Eventbus