EventBus是github上的一個第三方開發庫,其在github上的項目主頁地址:https://github.com/greenrobot/EventBus
EventBus的消息模型是消息發佈者/訂閱者機制。android
(1)EventBus是消息發佈者(發送消息)/訂閱者(接收消息)模式。EventBus的消息發佈十分靈活,能夠在工程代碼中的任意位置發送消息,EventBus 發佈消息只須要一行代碼便可實現: EventBus.getDefault().post(event); Event即爲本身定義的類的實例。git
(2)EventBus在接收消息的Activity(或Fragment)中初始化。一般在Android的Activity(或者Fragment)的onCreate裏面註冊,僅需一行代碼: EventBus.getDefault().register(this); 相似於註冊一個消息監聽Listener,完了不要忘記註銷EventBus,在onDestory裏面 EventBus.getDefault().unregister(this);github
(3)EventBus接收消息。 在Activity中根據代碼實際狀況寫一個EventBus的消息接收函數: public void onEventMainThread(MyEvent event); 而後,只要EventBus發送消息,就能夠在這裏接收到。app
EventBus的消息回調接收消息函數還有幾個: onEventMainThread:Main線程,這個與Android UI線程密切相關,不要阻塞它! onEventBackgroundThread:故名思議,後臺線程中接收處理。 onEventAsync:異步線程中接收處理。異步
1 package com.lixu.chuandi; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9 import android.widget.TextView; 10 import de.greenrobot.event.EventBus; 11 12 public class MainActivity extends Activity { 13 TextView tv; 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 tv = (TextView) findViewById(R.id.tv1); 20 Button button = (Button) findViewById(R.id.btn1); 21 EventBus.getDefault().register(this); 22 button.setOnClickListener(new OnClickListener() { 23 24 @Override 25 public void onClick(View v) { 26 Event event = new Event(); 27 event.a = "你好啊 我是:"; 28 event.b = "小新"; 29 30 EventBus.getDefault().post(event); 31 Intent intent = new Intent(MainActivity.this, MyAppService.class); 32 startService(intent); 33 34 } 35 }); 36 37 } 38 39 // 這裏,EventBus回調接受消息,而後更新Main UI的TextView 40 public void onEventMainThread(Event event) { 41 42 tv.setText(event.toString()); 43 } 44 45 @Override 46 protected void onDestroy() { 47 48 super.onDestroy(); 49 EventBus.getDefault().unregister(this); 50 Intent intent = new Intent(MainActivity.this, MyAppService.class); 51 stopService(intent); 52 } 53 }
1 package com.lixu.chuandi; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.IBinder; 6 import android.util.Log; 7 import de.greenrobot.event.EventBus; 8 9 public class MyAppService extends Service { 10 @Override 11 public void onCreate() { 12 super.onCreate(); 13 EventBus.getDefault().register(this); 14 } 15 16 @Override 17 public int onStartCommand(Intent intent, int flags, int startId) { 18 Event event = new Event(); 19 // 能夠隨意在工程中的任意位置發送消息給接受者 20 EventBus.getDefault().post(event.toString()); 21 return super.onStartCommand(intent, flags, startId); 22 } 23 24 @Override 25 public IBinder onBind(Intent intent) { 26 return null; 27 } 28 29 // 在後臺中接收消息 30 public void onEventBackgroundThread(Event event) { 31 32 Log.e("MyAppService收到消息:", event.toString()); 33 } 34 }
1 package com.lixu.chuandi; 2 3 public class Event { 4 public String a; 5 public String b; 6 @Override 7 public String toString() { 8 return a+b; 9 } 10 11 }