## (一).前言:
今天我們的項目繼續更新,今天我們主要講解消息總線EventBus的基本使用方法,后面一篇我們會從源碼的角度稍微分析一下實現過程。
FastDev4Android框架項目地址:[https://github.com/jiangqqlmj/FastDev4Android](https://github.com/jiangqqlmj/FastDev4Android)
## (二).簡介:
以前我們做組件間的消息分發更新,一般會采用觀察者模式,或者接口數據回調的相關方式。但是這樣的做法雖然可以解決我們的問題,但是組件之間的耦合比較嚴重,而且代碼也不易閱讀和相關維護。為了解決這樣的問題我們可以使用消息總線EventBus框架。
EventBus是一款針對Android優化的發布/訂閱事件總線。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,線程之間傳遞消息.優點是開銷小,代碼更優雅。以及將發送者和接收者解耦。EventBus開源站點地址:[https://github.com/greenrobot/EventBus](https://github.com/greenrobot/EventBus)。
整個訂閱和接受的架構如下圖:

EventBus的特點如下:
* 簡化組件間的消息通信
* 使得代碼更加簡潔
* 速度很快
* jar包非常小,不到50K
* 還有一些有點例如線程之間的通信,優先級等
## (三).使用方式
3.1.AndroidStudio進行Gradle配置如下:
~~~
compile 'de.greenrobot:eventbus:2.4.0'
~~~
3.2.事件對象定義
~~~
publicclass MessageEvent { /* Additional fields if needed */ }
~~~
3.3.在接收頁面進行注冊? ??
~~~
eventBus.register(this);
~~~
3.4.接收消息方法實現? ? ? ??
~~~
public voidonEvent(AnyEventType event) {/* Do something */};
~~~
3.5.消息發送
~~~
eventBus.post(event);
~~~
?????? OK上面是官方的使用說明,現在我們來具體使用一個實例來展示一下EventBus的基本使用。
## (四).具體事例
4.1.實現需求:在第一個Activity中有一個按鈕和一個TextView,然后點擊按鈕打開第二個Activity,在第二個Activity中有一個按鈕,點擊按鈕關閉當前第二個Activity,同時消息回調到第一個Activity中,在TextView中進行顯示。??

4.2.我們這邊需要兩個Activity布局
~~~
<?xmlversion="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="點擊打開第二個Activity"
android:id="@+id/button_one"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="45dp"
android:text="這邊顯示消息內容..."
android:id="@+id/textView_one" />
</LinearLayout>
~~~
~~~
<?xmlversion="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二個Activity的按鈕"
android:id="@+id/button_two"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="45dp"
android:text="當前為第二個Activity"
android:id="@+id/textView_two" />
</LinearLayout>
~~~
4.3.創建一個事件管理類:TestEventFirst.java
~~~
packagecom.chinaztt.fda.event;
/**
* 當前類注釋:EventBus測試 First事件類
* 項目名:FastDev4Android
* 包名:com.chinaztt.fda.event
* 作者:江清清 on 15/11/3 14:25
* 郵箱:jiangqqlmj@163.com
* QQ: 781931404
* 公司:江蘇中天科技軟件技術有限公司
*/
public classTestEventFirst {
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public TestEventFirst(String msg){
this.msg=msg;
}
}
~~~
4.4:注冊和取消注冊
使用EventBus.getDefault().register(this);進行注冊
使用EventBus.getDefault().unregister(this);進行取消注冊
4.5.消息發送
使用?EventBus.getDefault().post(new TestEventFirst("我是第二個Activity回傳的信息...."));進行消息發送
4.6.消息接收
在注冊的Activity中進行重寫onEventMainThread()方法來進行處理接收消息(除了這個方法以外,還有另外三個方法,具體我們會在下一篇文章中進行介紹)
~~~
/**
* 收到消息 進行相關處理
* @param event
*/
public voidonEventMainThread(TestEventFirst event) {
textView_one.setText(event.getMsg());
showToastMsgShort(event.getMsg());
}
~~~
???其中方法中的參數TestEventFirst就是發送過來的消息類,具體發送的消息全部已經封裝在里面了。我們只需要使用event對象進行獲取處理即可。
4.7.完整第一個Activity和第二個Activity代碼如下:
~~~
packagecom.chinaztt.fda.test;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.TextView;
importandroid.widget.Toast;
importcom.chinaztt.fda.event.TestEventFirst;
importcom.chinaztt.fda.ui.R;
importcom.chinaztt.fda.ui.base.BaseActivity;
importcom.chinaztt.fda.utils.Log;
importorg.androidannotations.annotations.Click;
importorg.androidannotations.annotations.EActivity;
importorg.androidannotations.annotations.ViewById;
importorg.w3c.dom.Text;
importde.greenrobot.event.EventBus;
/**
* 當前類注釋:EventBus組件間數據通信實例
* 項目名:FastDev4Android
* 包名:com.chinaztt.fda.test
* 作者:江清清 on 15/11/3 13:14
* 郵箱:jiangqqlmj@163.com
* QQ: 781931404
* 公司:江蘇中天科技軟件技術有限公司
*/
@EActivity
public classEventBusTestActivity extendsBaseActivity{
Button button_one;
TextView textView_one;
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event_bus_test_layout);
EventBus.getDefault().register(this);
button_one=(Button)this.findViewById(R.id.button_one);
textView_one=(TextView)this.findViewById(R.id.textView_one);
button_one.setOnClickListener(newView.OnClickListener() {
@Override
public void onClick(View v) {
openActivity(EventBusTestTwoActivity_.class);
}
});
}
/**
* 收到消息 進行相關處理
* @param event
*/
public voidonEventMainThread(TestEventFirst event) {
textView_one.setText(event.getMsg());
showToastMsgShort(event.getMsg());
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
~~~
~~~
packagecom.chinaztt.fda.test;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
importcom.chinaztt.fda.event.TestEventFirst;
importcom.chinaztt.fda.ui.R;
importcom.chinaztt.fda.ui.base.BaseActivity;
importorg.androidannotations.annotations.Click;
importorg.androidannotations.annotations.EActivity;
importorg.androidannotations.annotations.ViewById;
importde.greenrobot.event.EventBus;
/**
* 當前類注釋:
* 項目名:FastDev4Android
* 包名:com.chinaztt.fda.test
* 作者:江清清 on 15/11/3 14:25
* 郵箱:jiangqqlmj@163.com
* QQ: 781931404
* 公司:江蘇中天科技軟件技術有限公司
*/
@EActivity
public classEventBusTestTwoActivity extends BaseActivity {
Button button_two;
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event_bus_test_two_layout);
button_two=(Button)this.findViewById(R.id.button_two);
button_two.setOnClickListener(newView.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post(new TestEventFirst("我是第二個Activity回傳的信息...."));
EventBusTestTwoActivity.this.finish();
}
});
}
}
~~~
到此我們的EventBus的基本使用已經講完了,看一下上面的效果演示,具體深入詳解以及其他的幾個方法的介紹和相關源代碼分析會在下一篇文章中進行講解。
我們的項目已經配置集成了消息總線EventBus的例子.歡迎大家去Github站點進行clone或者下載瀏覽:[https://github.com/jiangqqlmj/FastDev4Android](https://github.com/jiangqqlmj/FastDev4Android)?同時歡迎大家star和fork整個開源快速開發框架項目~
- 前言
- Android快速開發框架介紹(一)
- Android首頁圖片自動無限循環輪播Gallery+FlowIndicator(二)
- Android 列表下拉刷新組件PullToRefreshListView使用(三)
- Android 數據緩存器ACache的詳解和使用(四)
- Android崩潰異常捕捉CustomCrash,提升用戶體驗(五)
- Android實現沉浸式狀態欄(六)
- AndroidAnnnotations注入框架介紹和Android Studios基本配置(七)
- AndroidAnnnotations注入框架的工作原理(八)
- AndroidAnnnotations注入框架使用之注入組件Components(九)
- AndroidAnnnotations注入框架使用之Injection標簽詳解(十)
- AndroidAnnnotations注入框架使用之事件綁定Event Binding(十一)
- AndroidAnnnotations注入框架使用之線程處理Threading(十二)
- AndroidAnnnotations注入框架使用之第三方框架集成RoboGuice(十三)
- AndroidAnnnotations注入框架使用之第三方框架集成Otto事件總線(十四)
- AndroidAnnnotations注入框架使用之第三方框架集成OrmLite(十五)
- AndroidAnnnotations注入框架使用之最佳實踐之Adapters和lists(十六)
- AndroidAnnnotations注入框架使用之最佳實踐SharedPreferences(十七)
- Android MVP開發模式詳解(十九)
- 消息總線EventBus的基本使用(二十)
- 消息總線EventBus源碼分析以及與Otto框架對比(二十一)
- 列表頭生成帶文本或者字母的圖片開源庫TextDrawable使用和詳解(二十二)
- 重寫WebView網頁加載以及JavaScript注入詳解(二十三)
- BaseAdapterHelper的基本使用介紹,讓你擺脫狂寫一堆Adapter煩惱(二十四)
- BaseAdapterHelper詳解源碼分析,讓你擺脫狂寫一堆Adapter煩惱(二十五)
- Volley完全解析之基礎使用(二十六)
- Volley完全解析之進階最佳實踐與二次封裝(二十七)
- RecyclerView完全解析,讓你從此愛上它(二十八)
- RecyclerView完全解析之打造新版類Gallery效果(二十九)
- RecyclerView完全解析之結合AA(Android Annotations)注入框架實例(三十)
- RecyclerView完全解析之下拉刷新與上拉加載SwipeRefreshLayout(三十一)
- CardView完全解析與RecyclerView結合使用(三十二)
- 神器ViewDragHelper完全解析,媽媽再也不擔心我自定義ViewGroup滑動View操作啦~(三十三)
- 神器ViewDragHelper完全解析之詳解實現QQ5.X側滑酷炫效果(三十四)
- 實例解析之SwipeRefreshLayout+RecyclerView+CardView(三十五)
- HorizontalScrollView,Fragment,FragmentStatePagerAdapter打造網易新聞Tab及滑動頁面效果(三十六)
- Android Design支持庫TabLayout打造仿網易新聞Tab標簽效果(三十七)
- 打造QQ6.X最新版本側滑界面效果(三十八)