消息總線EventBus的基本使用
(一).前言:
今天我們的項目繼續更新,今天我們主要講解消息總線EventBus的基本使用方法,后面一篇我們會從源碼的角度稍微分析一下實現過程。
FastDev4Android框架項目地址: https://github.com/jiangqqlmj/FastDev4Android
(二).簡介:
以前我們做組件間的消息分發更新,一般會采用觀察者模式,或者接口數據回調的相關方式。但是這樣的做法雖然可以解決我們的問題,但是組件之間的耦合比較嚴重,而且代碼也不易閱讀和相關維護。為了解決這樣的問題我們可以使用消息總線EventBus框架。
EventBus是一款針對Android優化的發布/訂閱事件總線。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,線程之間傳遞消息.優點是開銷小,代碼更優雅。以及將發送者和接收者解耦。EventBus開源站點地址: https://github.com/greenrobot/EventBus 。
整個訂閱和接受的架構如下圖:
EventBus的特點如下:
- 簡化組件間的消息通信
- 使得代碼更加簡潔
- 速度很快
- jar包非常小,不到50K
- 還有一些有點例如線程之間的通信,優先級等
(三).使用方式
3.1.AndroidStudio進行Gradle配置如下:
compile 'de.greenrobot:eventbus:2.4.0'
3.2.事件對象定義
public class MessageEvent { /* Additional fields if needed */ } 3.3.在接收頁面進行注冊
eventBus.register(this);
3.4.接收消息方法實現
public void onEvent(AnyEventType event) {/* Do something */}; 3.5.消息發送
eventBus.post(event);
OK上面是官方的使用說明,現在我們來具體使用一個實例來展示一下EventBus的基本使用。
(四).具體事例
4.1.實現需求:在第一個Activity中有一個按鈕和一個TextView,然后點擊按鈕打開第二個Activity,在第二個Activity中有一個按鈕,點擊按鈕關閉當前第二個Activity,同時消息回調到第一個Activity中,在TextView中進行顯示。
4.2.我們這邊需要兩個Activity布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: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> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: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
package com.chinaztt.fda.event;
/**
* 當前類注釋:EventBus測試 First事件類
* 項目名:FastDev4Android
* 包名:com.chinaztt.fda.event
* 作者:江清清 on 15/11/3 14:25
* 郵箱:jiangqqlmj@163.com
* QQ: 781931404
* 公司:江蘇中天科技軟件技術有限公司
*/
public class TestEventFirst {
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 void onEventMainThread(TestEventFirst event) {
textView_one.setText(event.getMsg());
showToastMsgShort(event.getMsg());
} 其中方法中的參數TestEventFirst就是發送過來的消息類,具體發送的消息全部已經封裝在里面了。我們只需要使用event對象進行獲取處理即可。
4.7.完整第一個Activity和第二個Activity代碼如下:
package com.chinaztt.fda.test;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.chinaztt.fda.event.TestEventFirst;
import com.chinaztt.fda.ui.R;
import com.chinaztt.fda.ui.base.BaseActivity;
import com.chinaztt.fda.utils.Log;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.w3c.dom.Text;
import de.greenrobot.event.EventBus;
/**
* 當前類注釋:EventBus組件間數據通信實例
* 項目名:FastDev4Android
* 包名:com.chinaztt.fda.test
* 作者:江清清 on 15/11/3 13:14
* 郵箱:jiangqqlmj@163.com
* QQ: 781931404
* 公司:江蘇中天科技軟件技術有限公司
*/
@EActivity
public class EventBusTestActivity extends BaseActivity{
Button button_one;
TextView textView_one;
@Override
protected void onCreate(Bundle savedInstanceState) {
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(new View.OnClickListener() {
@Override
public void onClick(View v) {
penActivity(EventBusTestTwoActivity_.class);
}
});
}
/**
* 收到消息 進行相關處理
* @param event
*/
public void onEventMainThread(TestEventFirst event) {
textView_one.setText(event.getMsg());
showToastMsgShort(event.getMsg());
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}package com.chinaztt.fda.test;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.chinaztt.fda.event.TestEventFirst;
import com.chinaztt.fda.ui.R;
import com.chinaztt.fda.ui.base.BaseActivity;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import de.greenrobot.event.EventBus;
/**
* 當前類注釋:
* 項目名:FastDev4Android
* 包名:com.chinaztt.fda.test
* 作者:江清清 on 15/11/3 14:25
* 郵箱:jiangqqlmj@163.com
* QQ: 781931404
* 公司:江蘇中天科技軟件技術有限公司
*/
@EActivity
public class EventBusTestTwoActivity extends BaseActivity {
Button button_two;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event_bus_test_two_layout);
button_two=(Button)this.findViewById(R.id.button_two);
button_two.setOnClickListener(new View.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 同時歡迎大家star和fork整個開源快速開發框架項目~
尊重原創,轉載請注明:From Sky丶清(http://www.lcode.org) 侵權必究!
關注我的訂閱號(codedev123),每天分享移動開發技術(Android/IOS),項目管理以及博客文章!(歡迎關注,第一時間推送精彩文章)
關注我的微博,可以獲得更多精彩內容