EventBus 学习
一.概述
EventBus是一款针对Android优化的发布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间传递消息.优点是开销小,代码更优雅。以及将发送者和接收者解耦。
实现主要有3个元素组成
1.事件:event
2.发布者:发出消息的一方
3.订阅者:接受并处理消息
二.使用步骤
1.下载EventBus库
地址:https://github.com/greenrobot/EventBus
1.添加gradle
compile 'de.greenrobot:eventbus:2.4.0'
2.下载库,解压,在项目中import modules,再设置dependence
2.自定义事件类
public class TestEvent {
private String mMesg;
public TestEvent(String mMesg) {
this.mMesg = mMesg;
}
public String getmMesg() {
return mMesg;
}
}
3.订阅者注册以及取消注册
注册:EventBus.getDefault().register(this);
取消:EventBus.getDefault().unregister(this);
4.发布者发送消息
EventBus.getDefault().post(new TestEvent("send mesg"));
5.订阅者消息处理
public void onEventMainThread()
三、实践
在MainActivity中点击button跳转至SecondActivity,在SecondActivity中点击button发送消息给MainActivity,并跳转至MainActivity,MainActivity处理实践,并且显示。
1.MainActivity布局
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/FirstActivity" />
<TextView
android:id="@+id/display_mesg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:background="#00ff00"
android:visibility="gone"/>
<Button
android:id="@+id/start_activity"
android:text="@string/start_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
2.SecondActivity布局
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.xuyushi.eventbustest.SecondActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/SecondActivity" />
<Button
android:id="@+id/send_mesg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/sendMesg"/>
</LinearLayout>
3.自定义Event
package com.example.xuyushi.eventbustest;
/**
* Created by xuyushi on 15/6/14.
*/
public class TestEvent {
private String mMesg;
public TestEvent(String mMesg) {
this.mMesg = mMesg;
}
public String getmMesg() {
return mMesg;
}
}
4.MainAcitivity 按键事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
}
});
5.SecondActivity 按键事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//send mesg to MainActivity
EventBus.getDefault().post(new TestEvent("send mesg"));
Toast.makeText(getApplicationContext(),"mesg have send",Toast.LENGTH_SHORT).show();
finish();
}
});
6.处理事件
public void onEventMainThread(TestEvent testEvent) {
String msg = testEvent.getmMesg();
Log.d("MainActivity", "msg have recevied:" + msg);
displayMesg.setText("msg have recevied:"+ msg);
displayMesg.setVisibility(View.VISIBLE);
}