第一步:在build.gradle中添加依赖
dependencies {
compile 'org.greenrobot:eventbus:3.0.0'
}
第二步:创建一个 Event类: 注意:enum 不是class
public enum Event {
//消息名,可以根据这个名字判断是哪个消息
IMAGE_LOADER_SUCCESS;
}
//------------主布局文件------------------------
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_text" />
<Button
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="点击跳转到TwoActivity"
android:id="@+id/bt_button"/>
//-----------TwoActivity布局-----------------------
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt_button2"
android:text="发送消息"/>
//---------创建一个消息了---MessageEvent ------------------------
public class MessageEvent {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public MessageEvent(String message) {
this.message = message;
}
public MessageEvent() {
}
}
//--------------MainActivity -中我们注册事件,用于接收消息,退出时需要注销-----------------
package eventbusdemo.wode.com.eventbusdemo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button bt_button;
private TextView tv_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到控件
bt_button = (Button) findViewById(R.id.bt_button);
tv_text = (TextView) findViewById(R.id.tv_text);
//在需要订阅事件的地方注册事件
EventBus.getDefault().register(this);
//设置监听
bt_button.setOnClickListener(this);
}
//用于接收传MessageEvent这个类的消息
@Subscribe(threadMode = ThreadMode.MAIN)
public void onShowMessageEvent(MessageEvent messageEvent) {
tv_text.setText("MainActivity:" + messageEvent.getMessage());
}
//用于接收没有传值的消息
/* @Subscribe
public void onEventMainThread(Event event){
switch (event){
case IMAGE_LOADER_SUCCESS:
Log.i("MainActivity--","IMAGE_LOADER_SUCCESS");
tv_text.setText("IMAGE_LOADER_SUCCESS");
break;
}
}*/
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_button:
Intent intent=new Intent(MainActivity.this,TwoActivity.class);
startActivity(intent);
break;
}
}
//(5)取消消息订阅
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
//--------------TwoActivity 中,用于发送消息------------------------
package eventbusdemo.wode.com.eventbusdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import org.greenrobot.eventbus.EventBus;
public class TwoActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
Button bt_button2= (Button) findViewById(R.id.bt_button2);
bt_button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//(3)产生事件,即发送消息
// EventBus.getDefault().post(Event.IMAGE_LOADER_SUCCESS);
//传值
EventBus.getDefault().post(new MessageEvent("哈哈哈哈哈"));
}
});
}
}