Android EventBus的基本使用

来源:互联网 发布:怎么查IP端口开关 编辑:程序博客网 时间:2024/06/05 04:39

怕自己以后忘了怎么用。。。

一.背景(直接从github直接抄过来)

github地址:https://github.com/greenrobot/EventBus

EventBus is a publish/subscribe event bus optimized for Android.

EventBus...

  • simplifies the communication between components
    • decouples event senders and receivers
    • performs well with Activities, Fragments, and background threads
    • avoids complex and error-prone dependencies and life cycle issues
  • makes your code simpler
  • is fast
  • is tiny (~50k jar)
  • is proven in practice by apps with 100,000,000+ installs
  • has advanced features like delivery threads, subscriber priorities, etc.

其实简单的说就是简化了各个模块见得通信,

优点1.解耦了发送者接收者

2.在Activity,fragment,后台线程运行良好

3.避免在处理生命周期中容易出现的一些问题


二.代码

//事件1

/** * Created by Administrator on 2016/3/29 0029. */public class EventOne {    private String mMsg;    public EventOne(String msg) {        // TODO Auto-generated constructor stub        mMsg = "MainEventEventOne:"+msg;    }    public String getMsg(){        return mMsg;    }}
//事件二

/** * Created by Administrator on 2016/3/29 0029. */public class EventTwo {    private String mMsg;    public EventTwo(String msg) {        // TODO Auto-generated constructor stub        mMsg = "MainEventTwo:" + msg;    }    public String getMsg() {        return mMsg;    }}
//事件三

/** * Created by Administrator on 2016/3/29 0029. */public class EventThree {    private String mMsg;    public EventThree(String msg) {        // TODO Auto-generated constructor stub        mMsg = "MainEventEventThree:" + msg;    }    public String getMsg() {        return mMsg;    }}
//MainActivity.java

public class MainActivity extends Activity implements View.OnClickListener {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        findViewById(R.id.bt_jump).setOnClickListener(this);        EventBus.getDefault().register(this);    }    public void onEventMainThread(EventOne event) {        System.out.println("xcqw   onEventMainThread收到了消息:" + event.getMsg());    }    public void onEventMainThread(EventTwo event) {        System.out.println("xcqw   onEventMainThread收到了消息:" + event.getMsg());    }    public void onEventMainThread(EventThree event) {        System.out.println("xcqw   onEventMainThread收到了消息:" + event.getMsg());    }    public void onEvent(EventTwo event) {        System.out.println("xcqw   onEvent收到了消息:" + event.getMsg());    }    @Override    protected void onDestroy() {        super.onDestroy();        EventBus.getDefault().unregister(this);    }    @Override    public void onClick(View v) {        startActivity(new Intent(this, SecondActivity.class));    }}
注意:这里接收消息,但是同样需要,注册和解注册EventBus.getDefault().register(this)EventBus.getDefault().unregister(this),否则接收不到消息

//SecondActivity.java

public class SecondActivity extends Activity implements View.OnClickListener {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_second);        findViewById(R.id.bt_one).setOnClickListener(this);        findViewById(R.id.bt_two).setOnClickListener(this);        findViewById(R.id.bt_three).setOnClickListener(this);        //eventbus注册        EventBus.getDefault().register(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.bt_one:                EventBus.getDefault().post(                        new EventOne("EventOne btn clicked"));                break;            case R.id.bt_two:                EventBus.getDefault().post(                        new EventTwo("EventTwo btn clicked"));                break;            case R.id.bt_three:                EventBus.getDefault().post(                        new EventThree("EventThree btn clicked"));                break;        }    }    public void onEventMainThread(EventOne item) {        System.out.println("xcqw Second  onEventMainThread收到了消息:" + item.getMsg());    }    @Override    protected void onDestroy() {        super.onDestroy();        //eventbus解注册        EventBus.getDefault().unregister(this);    }}




(图片待添加)

可以看出

点击eventone

 xcqw   onEventMainThread收到了消息:MainEventEventOne:EventOne btn clicked
 xcqw Second  onEventMainThread收到了消息:MainEventEventOne:EventOne btn clicked(这个是写在SecondActivity,也能接收到消息)

点击eventtwo
 xcqw   onEvent收到了消息:MainEventTwo:EventTwo btn clicked
 xcqw   onEventMainThread收到了消息:MainEventTwo:EventTwo btn clicked

点击eventthree
 xcqw   onEventMainThread收到了消息:MainEventEventThree:EventThree btn clicked

(1)现在说说几个常见问题

Q1:什么时候注册,什么时候不注册

A:不管是接收还是发送的activity和fragment都需要注册和解注册EventBus.getDefault().register(this)EventBus.getDefault().unregister(this)


Q2:靠的是什么接收,也就是为什么这个函数能接收,那么函数不行?

A:根据发送的参数和接收函数的参数是否一样,只要接收函数一样,全都能接收到,可以从点击EventOne中可以看出来,MainActivity和SecondActivity中

//发送者

EventBus.getDefault().post(        new EventOne("EventOne btn clicked"));

//MainActivity.java

public void onEventMainThread(EventOne event) {        System.out.println("xcqw   onEventMainThread收到了消息:" + event.getMsg());    }

//SecondActivity.java

 public void onEventMainThread(EventOne item) {        System.out.println("xcqw Second  onEventMainThread收到了消息:" + item.getMsg());    }

Q3:如果有多个接收同一时间的函数,会不会都执行?

A:从上一个问题就可以看出结果,有多少个都会执行


Q4:有四个函数分别代表什么意思

A:

EventBus包含4个ThreadMode:PostThread,MainThread,BackgroundThread,Async

MainThread我们已经不陌生了;我们已经使用过。

具体的用法,极其简单,方法名为:onEventPostThread, onEventMainThread,onEventBackgroundThread,onEventAsync即可

具体什么区别呢?

onEvent来接收事件,那么接收事件和分发事件在一个线程中执行

onEventMainThread代表这个方法会在UI线程执行

onEventPostThread代表这个方法会在当前发布事件的线程执行

BackgroundThread这个方法,如果在非UI线程发布的事件,则直接执行,和发布在同一个线程中。如果在UI线程发布的事件,则加入后台任务队列,使用线程池一个接一个调用。

Async 加入后台任务队列,使用线程池调用,注意没有BackgroundThread中的一个接一个

0 0