import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import de.greenrobot.event.*;
import de.greenrobot.event.EventBus;
/**
* EventBus的简单用法
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册EventBus
de.greenrobot.event.EventBus.getDefault().register(this);
}
//点击Button跳转到SecondActivity界面
public void clickBtn(View view) {
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
//经常使用:无论发送消息的方法是运行在哪个线程中,此方法都是运行在主线程中的
public void onEventMainThread(EventMsg msg){
String str = msg.getMsg();
Log.i("TAG","onEventMainThread---"+"str:"+str+"----thread:"+Thread.currentThread().getName());
}
//由发送消息的方法决定,如果发送消息的方法是运行在主线程中,此方法就会运行在主线程中,如果...子线程,此方法就会运行在子线程中
public void onEvent(EventMsg msg){
String str = msg.getMsg();
Log.i("TAG","onEvent---"+"str:"+str+"----thread:"+Thread.currentThread().getName());
}
//无论发送消息的方法是运行在哪个线程中,此方法都是运行在子线程中的
//如果发送消息的方法是运行在子线程中,此方法和发送消息的方法公用一个线程
//如果发送消息的方法是运行在主线程中,会单独开启线程
public void onEventBackgroundThread(EventMsg msg){
String str = msg.getMsg();
Log.i("TAG","onEventBackgroundThread---"+"str:"+str+"----thread:"+Thread.currentThread().getName());
}
//无论发送消息的方法是运行在哪个线程中,此方法都是运行在子线程中的,另开启一个子线程
public void onEventAsync(EventMsg msg){
String str = msg.getMsg();
Log.i("TAG","onEventAsync--"+"str:"+str+"----thread:"+Thread.currentThread().getName());
}
@Override
protected void onDestroy() {
super.onDestroy();
//反注册EventBus
EventBus.getDefault().unregister(this);
}
}
<pre name="code" class="java">/**
* Created by pk on 16/5/10.
* 类名随便取
*/
public class EventMsg {
private String msg;
public EventMsg(String msg) {
this.msg = msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import de.greenrobot.event.EventBus;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
//点击Button传递数据
public void clickBtn(View view) {
//发送消息
EventBus.getDefault().post(new EventMsg("我是Msg"));
Log.i("TAG",Thread.currentThread().getName()+"------SecondActivity-----");
finish();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转"
android:onClick="clickBtn"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickBtn"/>
</RelativeLayout>