compile "de.greenrobot:eventbus:2.4.0"
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"><TextViewandroid:layout_gravity="center"android:id="@+id/show_msg"android:text="@string/hello_world"android:layout_width="wrap_content"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/to_second_activity"android:layout_gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="跳转第二个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"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="发送一个消息"android:id="@+id/send_msg" /><Buttonandroid:id="@+id/btn_finish"android:text="销毁这个Activity,返回第一个Activity"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>activity_second2、创建一个类,构造方法参数不固定,随便写,空类也可以,用于传递消息,看具体需求
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,并且获取和处理消息
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 {//按钮,开启第二个Activityprivate Button to_second_activity;//文本,显示接收到的消息private TextView show_msg;@Overrideprotected 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);//点击按钮进入到第二个Activityto_second_activity.setOnClickListener(new View.OnClickListener() {@Overridepublic 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);}@Overrideprotected void onDestroy() {super.onDestroy();// 解除注册EventBus.getDefault().unregister(this);}}MainActivity.class4、在要发送消息的页面发送消息
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;@Overrideprotected 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() {@Overridepublic void onClick(View v) {EventBus.getDefault().post(new MyMessage("this is a message"));}});btn_finish.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {finish();}});}}SecondActivity.classEventBus其他知识说明:
onEvent:使用onEvent,那么该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,也就是说发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent方法中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。onEventMainThread:使用onEventMainThread,那么不论事件是在哪个线程中发布出来的,onEventMainThread都会在UI线程中执行,接收事件就会在UI线程中运行,这个在Android中是非常有用的,因为在Android中只能在UI线程中跟新UI,所以在onEvnetMainThread方法中是不能执行耗时操作的。onEventBackground:使用onEventBackgrond,那么如果事件是在UI线程中发布出来的,那么onEventBackground就会在子线程中运行,如果事件本来就是子线程中发布出来的,那么onEventBackground函数直接在该子线程中执行。onEventAsync:使用onEventAsync,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.2、如果有多个地方发送消息,并且有多个消息处理函数,怎么确定哪个消息处理方法处理哪些消息呢?