• 概述

    greenrobot的EventBus是一个非常流行的开源库
    EventBus是一款针对Android优化的发布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程间传递消息.优点是开销小,代码更优雅。以及将发送者和接收者解耦。
    源码:https://github.com/greenrobot/EventBus

  • 官方:EventBus is a publish/subscribe event bus optimized for Android.
    这里写图片描述

EventBus 主要分四种事件
1、onEvent
2、onEventMainThread
3、onEventBackgroundThread
4、onEventAsync


  • onEvent:如果使用onEvent作为订阅函数,那么该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,也就是说发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent方法中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。

  • onEventMainThread:如果使用onEventMainThread作为订阅函数,那么不论事件是在哪个线程中发布出来的,onEventMainThread都会在UI线程中执行,接收事件就会在UI线程中运行,这个在Android中是非常有用的,因为在Android中只能在UI线程中跟新UI,所以在onEvnetMainThread方法中是不能执行耗时操作的。

  • onEventBackground:如果使用onEventBackgrond作为订阅函数,那么如果事件是在UI线程中发布出来的,那么onEventBackground就会在子线程中运行,如果事件本来就是子线程中发布出来的,那么onEventBackground函数直接在该子线程中执行。
  • onEventAsync:使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.

Demo
EventBus 使用起来很简单,主要4个步骤:注册->发布订阅->接收订阅事件->注销
1;注册
EventBus.getDefault().register(this);
2:发布
EventBus.getDefault().post(new FirstEvent ( “android” ));
3:接收 (四种事件根据需求进行接收)
public void onEventMainThread(FirstEvent event) {}
4: 注销
EventBus.getDefault().unregister(this);

具体代码:

public class MainActivity extends Activity {

    Button btn;
    TextView tv;

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

        EventBus.getDefault().register(this);

        btn = (Button) findViewById(R.id.btn_click);
        tv = (TextView)findViewById(R.id.tvShow);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent( MainActivity.this
                        SecondActivity.class);
                startActivity(intent);
            }
        });
    }

    public void onEventMainThread(FirstEvent event) {
        String msg = "onEventMainThread收到了消息:" + event.getMsg();
        Log.d("harvic", msg);
        tv.setText(msg);
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }

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

SecondActivity:

public class SecondActivity extends Activity {
    private Button btn_FirstEvent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        btn_FirstEvent = (Button) findViewById(R.id.btn_first_event);

        btn_FirstEvent.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                EventBus.getDefault().post(
                        new FirstEvent("FirstEvent btn clicked"));
            }
        });
    }
}

注意:

EventBus.getDefault().post(
new FirstEvent(“FirstEvent btn clicked”));

发布事件:FirstEvent是自己定义的一个类
public class FirstEvent {

private String mMsg;
public FirstEvent(String msg) {
      // TODO Auto-generated constructor stub
      mMsg = msg;
}
public String getMsg(){
      return mMsg ;
}

}

接收订阅事件也要对应的匹配参数
public void onEventMainThread(FirstEvent event)

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐