日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android消息通信之无所不能的第三方开源项目EventBus

發布時間:2025/5/22 Android 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android消息通信之无所不能的第三方开源项目EventBus 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


Android消息通信之無所不能的第三方開源項目EventBus

在Android開發中,消息通信在開發過程中是比較重要但比較略微繁瑣的過程,比如,Activity與Fragment之間的消息通信,后臺Service與前臺Activity的消息通信,Fragment與Fragment之間的消息通信等等情況,Android本身提供的有完善的廣播、在Service中也有的Messenger、handler中處理message等等完備的解決方案,而第三方的開源庫EventBus同樣提供了幾乎無所不能、易于理解和使用的Android消息事件解決方案。
EventBus是github上的一個第三方開發庫,其在github上的項目主頁地址:https://github.com/greenrobot/EventBus
EventBus的消息模型是消息發布者/訂閱者機制。
使用EventBus之前需要到EventBus項目主頁上將庫項目(https://github.com/greenrobot/EventBus/tree/master/EventBus ,截止2015年10月26日更新時間,未來也許有變動)包拖下來,接著作為Eclipse的一個工程導入,然后作為Android的一個lib,在自己的項目中引用即可。
本文以一個簡單的代碼例子說明EventBus。附錄參考文章2、3、4說明了如何實現前臺Activity與后臺Service之間的消息通信,本文則在附錄參考文章4的基礎上改造,使用EventBus實現后臺Service往前臺Activity發送消息進而實現通信。
先丟出本文代碼:
首先要定義一個自己的Event類,這個類可以隨意定義,里面包括自己想要在消息通信中傳遞的各種復雜數據結構,本文則簡單些,MyEvent.java:

package zhangphil.service;//被EventBus類的定義可以隨意 //這里面可以傳遞更復雜、更重的數據結構 public class MyEvent {public int id;public String content;@Overridepublic String toString() {return "id:" + id + " content:" + content;} }



前臺Activity:

package zhangphil.service;import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import de.greenrobot.event.EventBus;public class MainActivity extends Activity {private TextView text;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);text = (TextView) findViewById(R.id.textView);// 一般在onCreate方法里面注冊EventBusEventBus.getDefault().register(this);startMyService();}// 這里,EventBus回調接受消息,然后更新Main UI的TextViewpublic void onEventMainThread(MyEvent event) {Log.d(this.getClass().getName(), "onEventMainThread:" + event.toString());text.setText(event + "");}// EventBus回調接受消息public void onEventBackgroundThread(MyEvent event) {Log.d(this.getClass().getName(), "onEventBackgroundThread:" + event.toString());}// EventBus回調接受消息public void onEventAsync(MyEvent event) {Log.d(this.getClass().getName(), "onEventAsync:" + event.toString());}@Overrideprotected void onDestroy() {super.onDestroy();// 注銷EventBusEventBus.getDefault().unregister(this);stopMyService();}private void startMyService() {Intent intent = new Intent(this, MyService.class);this.startService(intent);}private void stopMyService() {Intent intent = new Intent(this, MyService.class);this.stopService(intent);} }



后臺Service:

package zhangphil.service;import android.app.IntentService; import android.content.Intent; import android.os.IBinder; import android.util.Log; import de.greenrobot.event.EventBus;public class MyService extends IntentService {public MyService() {super("MyService");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {return super.onStartCommand(intent, flags, startId);}@Overrideprotected void onHandleIntent(Intent intent) {for (int i = 0; i < 10; i++) {MyEvent event = new MyEvent();event.id = i;event.content = "數據" + i;// 可以隨意在工程代碼中的任意位置發送消息給訂閱者EventBus.getDefault().post(event);Log.d(this.getClass().getName(), "發送消息:" + event.toString());try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}}}@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onDestroy() {Log.d(this.getClass().getName(), "onDestroy");} }


說明:
(1)EventBus是消息發布者(發送消息)/訂閱者(接收消息)模式。EventBus的消息發布十分靈活,可以在工程代碼中的任意位置發送消息。在本例中,EventBus在Service的onHandleIntent循環發布10條消息。EventBus 發布消息只需要一行代碼即可實現:
EventBus.getDefault().post(event);
Event即為自己定義的類的實例。


(2)EventBus在接收消息的Activity(或Fragment)中初始化。在本例中是一個Activity接收消息,
首先,在Activity里面注冊EventBus,通常在Android的Activity(或者Fragment)的onCreate里面注冊,僅需一行代碼:
EventBus.getDefault().register(this);
類似于注冊一個消息監聽Listener,完了不要忘記注銷EventBus,在onDestory里面
EventBus.getDefault().unregister(this);

(3)EventBus接收消息。
在Activity中根據代碼實際情況寫一個EventBus的消息接收函數:
public void onEventMainThread(MyEvent event);
然后,只要EventBus發送消息,就可以在這里接收到。


?
EventBus的消息回調接收消息函數還有幾個:
onEventMainThread:Main線程,這個與Android UI線程密切相關,不要阻塞它!
onEventBackgroundThread:故名思議,后臺線程中接收處理。
onEventAsync:異步線程中接收處理。


附錄參考文章:
文章1:《Android Service簡介(系列1)》鏈接地址:http://blog.csdn.net/zhangphil/article/details/49373939
文章2:《Android Activity與Service數據交互:Binder、bindService(系列2)》鏈接地址:http://blog.csdn.net/zhangphil/article/details/49385005
文章3:《Android Service之串行化Service:IntentService(系列3)》鏈接地址:http://blog.csdn.net/zhangphil/article/details/49387139
文章4:《Android Service進程間雙向通信之Messenger(系列4)》http://blog.csdn.net/zhangphil/article/details/49402869
文章5:《Android進程間通信(IPC)的AIDL機制:Hello World示例 》鏈接地址:http://blog.csdn.net/zhangphil/article/details/43876657

總結

以上是生活随笔為你收集整理的Android消息通信之无所不能的第三方开源项目EventBus的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。