EventBus3.0简单介绍(代码)

build.gradle中添加引用

compile 'org.greenrobot:eventbus:3.0.0'

基本代码

SecondActivity 执行事件发布
public class SecondActivity extends AppCompatActivity implements View.OnClickListener {
  private  Button btnMain;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        btnMain = (Button) findViewById(R.id.btnMain);
        btnMain.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnMain:
                EventBus.getDefault().post(new MainMessage("传递信息:aikaifa"));//事件发布
                //此处可用EventBus.getDefault().post(String),方便在订阅处(@Subscriber)进行判断,可自定义static final String类型,自定义类似注册登录等的判断,方便做判断
                finish();
                break;
        }
    }
}
MainActivity执行事件订阅
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnMain, btnBackground, btnAsync, btnPosting, btn1;
    private TextView tv_desc;

    private String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//必须进行EventBus注册
        EventBus.getDefault().register(this);
        initView();
        initClick();

    }

    /**
     * 按钮点击
     */
    private void initClick() {
        btnMain.setOnClickListener(this);
        btnBackground.setOnClickListener(this);
        btnAsync.setOnClickListener(this);
        btnPosting.setOnClickListener(this);
        btn1.setOnClickListener(this);
    }

    /**
     * 初始化View
     */
    private void initView() {

        btnMain = (Button) findViewById(R.id.btnMain);
        btnBackground = (Button) findViewById(R.id.btnBackground);
        btnAsync = (Button) findViewById(R.id.btnAsync);
        btnPosting = (Button) findViewById(R.id.btnPosting);

        btn1 = (Button) findViewById(R.id.btn1);
        tv_desc = (TextView) findViewById(R.id.tv_desc);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//必须执行EventBus取消注册操作
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnMain:
                EventBus.getDefault().post(new MainMessage("MainMessage"));
                break;
            case R.id.btnBackground:
                EventBus.getDefault().post(new BackgroundMessage("BackgroundMessage"));
                break;
            case R.id.btnAsync:
                EventBus.getDefault().post(new AsyncMessage("AsyncMessage"));
                break;
            case R.id.btnPosting:
                EventBus.getDefault().post(new PostingMessage("PostingMessage"));
                break;
            case R.id.btn1:
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
                break;

        }
    }

//以下必须在方法前进行注解@Subscribe
/**  @Subscribe
 *   public void onMainEventBus(String msg) {
 *      //可以通过msg进行判断
 *      if(msg.equals(**)){
 *       //进行判断,判断是登录还是注册还是其他自定义操作
 *      }
 *      
 *   }
 **/

    //主线程中执行
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMainEventBus(MainMessage msg) {
        Log.e(TAG, msg.getMessage());
        tv_desc.setText(msg.getMessage());
    }

    //后台线程
    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    public void onBackgroundEventBus(BackgroundMessage msg) {
        Log.e(TAG, msg.getMessage());
    }

    //异步线程
    @Subscribe(threadMode = ThreadMode.ASYNC)
    public void onAsyncEventBus(AsyncMessage msg) {
        Log.e(TAG, msg.getMessage());

    }

    //默认情况,和发送事件在同一个线程
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onPostEventBus(PostingMessage msg) {

        Log.e(TAG, msg.getMessage());
    }
}

你可能感兴趣的:(EventBus3.0简单介绍(代码))