Simple implementation of EventBus and simple implementation of EventBus
EventBus is used in recent projects. It only has some simple functions, but it feels quite powerful. Code link: http://download.csdn.net/detail/qq_29774291/9629346
EventBus is the event processing mechanism of Guava and is an elegant Implementation of the observer mode (production/consumer programming model) in the design model. The main function is to replace Intent, Handler, and BroadCast to transmit messages between Fragment, Activity, Service, and threads. The advantage is that the overhead is small and the code is more elegant. And decouple the sender and receiver.
I. Download the Class Library: https://github.com/greenrobot/EventBus
Ii. Basic usage:
1. register on the page where you want to receive messages:
1 EventBus.getDefault().register(this);
2. Send a message on the sending interface:
Student student = new Student (20, "Zhang San", "male"); EventBus. getDefault (). post (student );
3. Accept information on the acceptance page:
@ Subscribe public void onMessageReviced (final Student Message) {// display a pop-up window System. out. println ("received Message dddx:" + Message); if (Message! = Null) {showAlertDialog (this, "Name:" + Message. getName () + "\ age:" + Message. getAge () + "\ Gender:" + Message. getSex ());}}
4. cancel registration:
@Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); EventBus.getDefault().unregister(this); }
Main program code
1 package com. item. jiejie. event; 2 3 import de. greenrobot. event. eventBus; 4 import de. greenrobot. event. subscribe; 5 import android. OS. bundle; 6 import android. app. activity; 7 import android. app. alertDialog; 8 import android. content. context; 9 import android. content. dialogInterface; 10 import android. content. intent; 11 import android. view. view; 12 import android. widget. button; 13 import android. widget. T ExtView; 14 15 public class MainActivity extends Activity {16 17 private TextView TV _View; 18 private Button btn_to_finish; 19 20 @ Override21 protected void onCreate (Bundle savedInstanceState) {22 super. onCreate (savedInstanceState); 23 setContentView (R. layout. activity_main); 24 setView (); 25 EventBus. getDefault (). register (this); 26} 27 28 @ Override29 protected void onDestroy () {30 // TODO Auto-generated Method stub31 super. onDestroy (); 32 EventBus. getDefault (). unregister (this); 33} 34 private void setView () {35 // TODO Auto-generated method stub36 TV _View = (TextView) findViewById (R. id. TV _text); 37 btn_to_finish = (Button) findViewById (R. id. btn_to_one); 38 btn_to_finish.setOnClickListener (new View. onClickListener () {39 40 @ Override41 public void onClick (View arg0) {42 // TODO Auto-generated method st Ub43 startActivity (new Intent (MainActivity. this, OneActivity. class); 44} 45}); 46} 47 private boolean isShow = false; 48 @ Override49 protected void onStart () {50 // TODO Auto-generated method stub51 super. onStart (); 52 isShow = true; 53} 54 @ Override55 protected void onStop () {56 // TODO Auto-generated method stub57 super. onStop (); 58 isShow = false; 59} 60 @ Subscribe61 public void onMessageReviced (fina L String Message) {62 TV _View.setText (Message); 63 System. out. println ("received Message ddd:" + Message); 64} 65 66 @ Subscribe67 public void onMessageReviced (final Student Message) {68 // display a pop-up window 69 System. out. println ("Receive Message dddx:" + Message); 70 if (Message! = Null) {71 showAlertDialog (this, "Name:" + Message. getName () + "\ age:" + Message. getAge () + "\ Gender:" + Message72. getSex (); 73} 74} 75 private void showAlertDialog (final Context context, String string) {76 AlertDialog. builder dialog = new AlertDialog. builder (context); 77 dialog. setTitle ("I am the title"); 78 dialog. setMessage (string); 79 dialog. setPositiveButton ("OK", new DialogInterface. onClickListener () {80 81 @ Override82 public void onClick (DialogInterface arg0, int arg1) {83 // TODO Auto-generated method stub84 85} 86}); 87 AlertDialog mDialog = dialog. create (); 88 mDialog. show (); 89} 90}