EventBus黏性事件

先导依赖

implementation 'org.greenrobot:eventbus:3.0.0'

然后写一个类EventBusMessage

public class EventBusMessage {
    public String name;
    public String pass;
    public String userId;
    public String sessionId;

    public EventBusMessage(String name, String pass,String userId,String sessionId) {
        this.name = name;
        this.pass = pass;
        this.userId=userId;
        this.sessionId=sessionId;


    }
    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }
}

然后把值传过去

 EventBus.getDefault().postSticky(new EventBusMessage("","",sessionId+"",userId+""));

在另一边进行接收

 //eventbus 把数据传过来
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void reception(EventBusMessage eventBusMessage) {
        userId = eventBusMessage.getUserId();
        sessionId = eventBusMessage.getSessionId();
        Log.e("好","userid:"+userId+"sesion:"+sessionId);

    }

不要忘了注册和取消注册

   //注册eventbus
        if(!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }

你可能感兴趣的:(EventBus黏性事件)