EventBus使用

  1. 关于EventBus

2.1)build.gradle 加入依赖

implementation 'org.greenrobot:eventbus:3.0.0'

2.2)定义一个类,用户数据传输中间件

public class MessageEvent{

    private String message;

    public  MessageEvent(String message){

        this.message=message;

    }

    public String getMessage() {

        return message;

    }

 

    public void setMessage(String message) {

        this.message = message;

    }

}

2.3)在一个Activity中,注册Event

EventBus.getDefault().register(this)

2.4)在同一个类中 做一个Subscribe,就是订阅,其实就是接收数据

 

@Subscribe(threadMode = ThreadMode.MAIN)

Public void receiveMsg(MessageEvent messageEvent){

       Tv_event.setText(messageEvent.getMessage());

}

2.5)EventBus应该随着Activity的销毁而销毁

protected void onDestroy() {

        super.onDestroy();

        EventBus.getDefault().unregister(this);

    }

 

2.5)在另外一个Activity中把这个Message发送出来

EventBus.getDefault().post(new MessageBus(“this is message”));

 

ThreadMode.POSTING ThreadMode.MAIN 是可以更新UI

ThreadMode.BACKGROUND  ThreadMode.ASYNC 是在线程中的(不能更新UI),但是我操作为什么线程中能更新UI了,难道是post()不放到Thread中,更新bakground async更新UI有问题,但是如果post放到Thread中,ui能更新?

 


public class MyEventMainActivity extends Activity implements View.OnClickListener {

    TextView
tv_eventmsg_posting;
    TextView
tv_eventmsg_main;
    TextView
tv_eventmsg_BACKGROUND;
    TextView
tv_eventmsg_ASYNC;

    Button
btn_registevent;

   
@Override
   
protected void onCreate(@Nullable Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
       
this.setContentView(R.layout.activity_myevent_main);

        initView();
        registerEvent();
    }

   
private void initView() {
       
tv_eventmsg_posting = this.findViewById(R.id.tv_eventmsg_posting);
       
tv_eventmsg_main = this.findViewById(R.id.tv_eventmsg_main);
       
tv_eventmsg_BACKGROUND = this.findViewById(R.id.tv_eventmsg_BACKGROUND);
       
tv_eventmsg_ASYNC = this.findViewById(R.id.tv_eventmsg_ASYNC);

       
btn_registevent = this.findViewById(R.id.btn_registevent);
       
btn_registevent.setOnClickListener(this);
    }

   
/**
     *
注册event
     */
   
private void registerEvent(){
        EventBus.getDefault().register(
this);
    }

   
/**
     *
接收messageEvent
     * @param
messageEvent
    
*/
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    @Subscribe(threadMode = ThreadMode.POSTING)
   
@Subscribe(threadMode = ThreadMode.POSTING)
   
public void recieveEventMsgPosting(MessageEvent messageEvent){
       
tv_eventmsg_posting.setText("POSTING:"+messageEvent.getMessage());
    }

   
@Subscribe(threadMode = ThreadMode.MAIN)
   
public void recieveEventMsgMain(MessageEvent messageEvent){
       
tv_eventmsg_main.setText("main:"+messageEvent.getMessage());
    }

   
@Subscribe(threadMode = ThreadMode.BACKGROUND)
   
public void recieveEventMsgBackground(MessageEvent messageEvent){
       
tv_eventmsg_BACKGROUND.setText("background:"+messageEvent.getMessage());
    }

   
@Subscribe(threadMode = ThreadMode.ASYNC)
   
public void recieveEventMsgAsync(MessageEvent messageEvent){
       
tv_eventmsg_ASYNC.setText("async:"+messageEvent.getMessage());
    }

   
@Override
   
public void onClick(View v) {
       
if(v.getId()==R.id.btn_registevent){
            Intent intent =
new Intent(this,MessagePublishActivity.class);
           
this.startActivity(intent);
        }
    }

   
@Override
   
protected void onDestroy() {
       
super.onDestroy();
        EventBus.getDefault().unregister(
this);
    }
}

 

 

public class MessagePublishActivity extends Activity implements View.OnClickListener {



    Button btn_publishevent;

    Button btn_publishevent_thread;

    @Override

    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.activity_messagepublish);

        initView();

    }



    private void initView() {

        btn_publishevent = this.findViewById(R.id.btn_publishevent);

        btn_publishevent_thread = this.findViewById(R.id.btn_publishevent_thread);

        btn_publishevent.setOnClickListener(this);

        btn_publishevent_thread.setOnClickListener(this);

    }



    @Override

    public void onClick(View v) {

        if(v.getId() == R.id.btn_publishevent){

            EventBus.getDefault().post(new MessageEvent("我是 event 发布者,也就是发送者"));

            finish();

        }else if(v.getId() == R.id.btn_publishevent_thread){

            new Thread(new Runnable() {

                @Override

                public void run() {

                    EventBus.getDefault().post(new MessageEvent("我是 event 发布者,也就是发送者thread"));

                    finish();

                }

            }).start();



        }

    }

}

你可能感兴趣的:(android)