第一步
implementation 'org.greenrobot:eventbus:3.1.1'
第二步:书写实体
public class BluToothEvent {
private String message;
public BluToothEvent(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;}
}
第三步:保存值到实体里面去
EventBus.getDefault().post(new BluToothEvent("btphone"));
第四步:注册取值(一般放在oncreate里面,一定要写判断防止已经注册过了的问题)
if (!EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().register(this);
}
第五步:取值(方法名字可以随便写,标示一定要是@Subscribe(threadMode = ThreadMode.MAIN))
@Subscribe(threadMode = ThreadMode.MAIN)
public void openBluTooth(BluToothEvent bluToothEvent) {
if (bluToothEvent.getMessage().equals("btphone")) {
currentString = "btphone";
} else if (bluToothEvent.getMessage().equals("music")) {
currentString = "music";
}
}