淺談EventBus
概述:
EventBus是一款針對Android優(yōu)化的發(fā)布/訂閱事件總線。
主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service。
線程之間傳遞消息.優(yōu)點(diǎn)是開銷小,代碼更優(yōu)雅,以及將發(fā)送者和接收者解耦。
使用:
build.gradle ,如果這種方式 不需要下載類庫或者jar包 一句話即可導(dǎo)入
compile 'de.greenrobot:eventbus:2.4.0'
一、EventBus的使用,簡單的來說就是5步:創(chuàng)建一個類(具體使用下面介紹),注冊,發(fā)送消息,接收消息,解除注冊
看一個Demo:
實(shí)現(xiàn)功能:有兩個Activity,第一個Activity 跳轉(zhuǎn)第二個Activity,第二個Activity 點(diǎn)擊按鈕發(fā)送消息,第一個Activity中的TextView顯示接收到的這個消息信息

1、寫下兩個Activity的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_gravity="center"
android:id="@+id/show_msg"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/to_second_activity"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳轉(zhuǎn)第二個Activity"/>
</LinearLayout>
activity_main
<?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="發(fā)送一個消息"
android:id="@+id/send_msg" />
<Button
android:id="@+id/btn_finish"
android:text="銷毀這個Activity,返回第一個Activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
activity_second
2、創(chuàng)建一個類,構(gòu)造方法參數(shù)不固定,隨便寫,空類也可以,用于傳遞消息,看具體需求
package com.xqx.com.eventbusdemo;
public class MyMessage {
private String string;
public MyMessage(String string) {
this.string = string;
}
public String getString() {
return string;
}
}
3、在你接收消息的頁面(第一個Activity)注冊和解除注冊EventBus,并且獲取和處理消息
在onCreate()方法中注冊
EventBus.getDefault().register(this);
在onDestroy()方法中取消注冊
EventBus.getDefault().unregister(this);
實(shí)現(xiàn)獲取處理消息的方法,這里先使用onEventMainThread()方法,意思為接收到消息并在UI線程操作
public void onEventMainThread(MyMessage event) {
String msg = "onEventMainThread收到了消息:" + event.getString();
show_msg.setText(msg);
}
完整代碼:
package com.xqx.com.eventbusdemo;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import de.greenrobot.event.EventBus;
public class MainActivity extends Activity {
//按鈕,開啟第二個Activity
private Button to_second_activity;
//文本,顯示接收到的消息
private TextView show_msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
to_second_activity = (Button) findViewById(R.id.to_second_activity);
show_msg = (TextView) findViewById(R.id.show_msg);
//注冊
EventBus.getDefault().register(this);
//點(diǎn)擊按鈕進(jìn)入到第二個Activity
to_second_activity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,SecondActivity.class));
}
});
}
//接收消息并處理
public void onEventMainThread(MyMessage event) {
String msg = "onEventMainThread收到了消息:" + event.getString();
show_msg.setText(msg);
}
@Override
protected void onDestroy() {
super.onDestroy();
// 解除注冊
EventBus.getDefault().unregister(this);
}
}
MainActivity.class
4、在要發(fā)送消息的頁面發(fā)送消息
發(fā)送消息很簡單,參數(shù)是你自己寫的那個類
EventBus.getDefault().post(new MyMessage("this is a message"));
完整代碼:
package com.xqx.com.eventbusdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import de.greenrobot.event.EventBus;
public class SecondActivity extends Activity{
private Button send_msg;
private Button btn_finish;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
send_msg = (Button) findViewById(R.id.send_msg);
btn_finish = (Button) findViewById(R.id.btn_finish);
send_msg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post(new MyMessage("this is a message"));
}
});
btn_finish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
SecondActivity.class
EventBus其他知識說明:
1、EventBus有四個不同的消息接收處理方法:
onEvent: 使用onEvent,那么該事件在哪個線程發(fā)布出來的,onEvent就會在這個線程中運(yùn)行,也就是說發(fā)布事件和接收事件線程在同一個線程。使用這個方法時,在onEvent方法中不能執(zhí)行耗時操作,如果執(zhí)行耗時操作容易導(dǎo)致事件分發(fā)延遲。 onEventMainThread: 使用onEventMainThread,那么不論事件是在哪個線程中發(fā)布出來的,onEventMainThread都會在UI線程中執(zhí)行,接收事件就會在UI線程中運(yùn)行,這個在Android中是非常有用的,因為在Android中只能在UI線程中跟新UI,所以在onEvnetMainThread方法中是不能執(zhí)行耗時操作的。 onEventBackground: 使用onEventBackgrond,那么如果事件是在UI線程中發(fā)布出來的,那么onEventBackground就會在子線程中運(yùn)行,如果事件本來就是子線程中發(fā)布出來的,那么onEventBackground函數(shù)直接在該子線程中執(zhí)行。 onEventAsync: 使用onEventAsync,那么無論事件在哪個線程發(fā)布,都會創(chuàng)建新的子線程在執(zhí)行onEventAsync.
2、如果有多個地方發(fā)送消息,并且有多個消息處理函數(shù),怎么確定哪個消息處理方法處理哪些消息呢?
這就看四個消息處理方法的參數(shù)。發(fā)送消息的參數(shù)是某一個類,接收的也必須是這個類,否則接收不到。如有有多個OnEvent()方法參數(shù)相同,那么這些方法都可以接收到消息。
---------------------------------------------------------------------------------------
總結(jié):
register(注冊)會把當(dāng)前類中匹配的方法(onEvent開頭的),存入一個map,而post會根據(jù)實(shí)參去map查找進(jìn)行反射調(diào)用
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,同時也希望多多支持腳本之家!
相關(guān)文章
Android編程實(shí)現(xiàn)對話框形式進(jìn)度條功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)對話框形式進(jìn)度條功能,結(jié)合具體實(shí)例形式分析了Android對話框形式進(jìn)度條的功能與布局相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09
Android中Listview下拉刷新和上拉加載更多的多種實(shí)現(xiàn)方案
本文大概通過三種方案給大家介紹了Android中Listview下拉刷新和上拉加載更多知識,非常不錯,具有參考借鑒價值,需要的朋友參考下2016-12-12
ExpandableListView實(shí)現(xiàn)簡單二級列表
這篇文章主要為大家詳細(xì)介紹了ExpandableListView實(shí)現(xiàn)簡單二級列表,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11
Android中AsyncTask的入門使用學(xué)習(xí)指南
AsyncTask異步任務(wù),用于執(zhí)行耗時任務(wù)并在UI線程中更新結(jié)果。下面這篇文章主要給大家介紹了關(guān)于Android中AsyncTask入門使用的相關(guān)資料,需要的朋友可以參考下2019-02-02
Android端實(shí)現(xiàn)單點(diǎn)登錄的方法詳解
所謂單點(diǎn)登錄就是指的同一個賬戶(id)不能在一個以上的設(shè)備上登錄對應(yīng)的用戶系統(tǒng)(排除web端和移動端可以同時登錄的情況),例如:用戶m在A設(shè)備登錄并保持登錄狀態(tài),然后又在B設(shè)備登錄,此時A應(yīng)該要強(qiáng)制下線,m無法在A設(shè)備上繼續(xù)執(zhí)行用戶相關(guān)的操作,下面來一起看看吧。2016-11-11
Android實(shí)現(xiàn)網(wǎng)絡(luò)多線程文件下載
這篇文章主要介紹了Android實(shí)現(xiàn)網(wǎng)絡(luò)多線程文件下載的相關(guān)資料,需要的朋友可以參考下2016-03-03

