1.添加依赖
compile 'org.greenrobot:eventbus:3.0.0'
2.自定义事件类,并提供构造方法以及get方法
public class SecondEvent {
public String mMsg;
public SecondEvent(String mMsg) {
this.mMsg = mMsg;
}
public String getmMsg() {
return mMsg;
}
}
//在需要接收消息的页面注册EventBus
EventBus.getDefault().register(this);
4.在需要接收消息的类中反注册EventBus
@Override
protected void onDestroy() {
super.onDestroy();
//避免忘记,所以接着在onDestory()中反注册EventBus
EventBus.getDefault().unregister(this);
}
//发送消息到MainActivity
EventBus.getDefault().post(new SecondEvent("我发消息过来了"));
6.在要接收消息的类中接收消息
//接收从SecondActivity传递过来的消息
//注意此处的EventBus是3.0.0需要加注释
@Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
public void onEventMainThread(SecondEvent secondEvent){
String msg="MainActivity收到了消息:"+secondEvent.getmMsg();
tv.setText(msg);
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
经过这六步就大功告成了
下面贴上每个类的完整代码:
activity_main.xml
<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:orientation="vertical">
<Button
android:id="@+id/btn_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MainActivity Button"/>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<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:orientation="vertical">
<Button
android:id="@+id/btn_secord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SecordButton Button"/>
</LinearLayout>
public class SecondEvent {
public String mMsg;
public SecondEvent(String mMsg) {
this.mMsg = mMsg;
}
public String getmMsg() {
return mMsg;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button btn;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.在需要接收消息的页面注册EventBus
EventBus.getDefault().register(this);
tv = (TextView) findViewById(R.id.tv);
btn = (Button) findViewById(R.id.btn_main);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
}
});
}
//接收从SecondActivity传递过来的消息
//注意此处的EventBus是3.0.0需要加注释
@Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
public void onEventMainThread(SecondEvent secondEvent) {
String msg = "MainActivity收到了消息:" + secondEvent.getmMsg();
tv.setText(msg);
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
//2.避免忘记,所以接着反注册EventBus
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
SecondActivity.java
public class SecondActivity extends AppCompatActivity{
private Button btn_second;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
btn_second = (Button) findViewById(R.id.btn_secord);
btn_second.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//3.发送消息到MainActivity
EventBus.getDefault().post(new SecondEvent("我发消息过来了"));
}
});
}
}
Demo下载地址:http://download.csdn.net/detail/k2514091675/9793736