EventBus

转载:https://blog.csdn.net/zzb_bin/article/details/80229456

https://www.jianshu.com/p/f9ae5691e1bb

注册事件

1 if (isEventBus) {
2             if (!EventBus.getDefault().isRegistered(this)) {
3                 EventBus.getDefault().register(this);
4             }
5         }

注销事件

1 if (isEventBus) {
2             if (EventBus.getDefault().isRegistered(this)) {
3                 EventBus.getDefault().unregister(this);
4             }

发送事件(此处是在设置界面关闭时(onDestroy中)发送事件)

1 protected void onDestroy() {
2         super.onDestroy();
3         EventBus.getDefault().post(new TimeEventBean("1"));
4     }

定义类TimeEventBean

 1 public class TimeEventBean {
 2     private String msg;
 3 
 4     public TimeEventBean() {
 5     }
 6 
 7     public TimeEventBean(String msg) {
 8         this.msg = msg;
 9     }
10 
11     public String getMsg() {
12         return msg;
13     }
14 
15     public void setMsg(String msg) {
16         this.msg = msg;
17     }
18 }

接收消息并对其进行处理

1 @Subscribe(threadMode = ThreadMode.MAIN)
2     public void onTimeEvent(TimeEventBean event) {
3         //处理主页的event事件
4         KLog.d(event.getMsg());
5         if (event.getMsg().equals("1")){
6             KLog.d("事件1,调用isParentSettingTrue()");
7             isParentSettingTrue();
8         }
9     }
原文地址:https://www.cnblogs.com/ken9527just/p/11367959.html