EventBus使用步骤(二):黏性事件(每天学一点)

前提引入EventBus

 ompile 'org.greenrobot:eventbus:3.0.0'

一、消息处理类

package com.shangyu.eventbusdemo;

/**
 * Created by 黄家三少 on 2016/9/24.
 */
public class StickyMessage {
    private int msg;

    public StickyMessage(int msg) {
        this.msg = msg;
    }

    public void setMsg(int msg) {
        this.msg = msg;
    }

    public int getMsg() {
        return msg;
    }

}

二、MainActivity

package com.shangyu.eventbusdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

@InjectView(R.id.btn_register)
Button btnRegister;
@InjectView(R.id.btn_post)
Button btnPost;
@InjectView(R.id.btn_unregister)
Button btnUnregister;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.inject(this);
    btnRegister.setOnClickListener(this);
    btnPost.setOnClickListener(this);
    btnUnregister.setOnClickListener(this);
}

private int msg = 0;

@Override
public void onClick(View v) {
    switch (v.getId()) {
        /**
         * 订阅消息
         */
        case R.id.btn_register:
            EventBus.getDefault().register(this);
            break;
        /**
         * 发送消息
         */
        case R.id.btn_post:
            EventBus.getDefault().postSticky(new StickyMessage(msg++));
            break;
        /**
         * 取消订阅
         */
        case R.id.btn_unregister:
            EventBus.getDefault().unregister(this);
            break;
    }
}
@Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
public  void onMessageEventPostThread(StickyMessage stickyMessage){
    Log.i("ThreadMode.POSTING","ThreadMode.POSTING===>"+stickyMessage.getMsg());
}
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public  void onMessageEventMainThread(StickyMessage stickyMessage){
    Log.i("ThreadMode.MAIN","ThreadMode.MAIN===>"+stickyMessage.getMsg());
}
@Subscribe(threadMode = ThreadMode.BACKGROUND, sticky = true)
public  void onMessageEventBackgroundThread(StickyMessage stickyMessage){
    Log.i("ThreadMode.BACKGROUND","ThreadMode.BACKGROUND===>"+stickyMessage.getMsg());
}
@Subscribe(threadMode = ThreadMode.ASYNC, sticky = true)
public  void onMessageEventAsync(StickyMessage stickyMessage){
    Log.i("ThreadMode.ASYNC","ThreadMode.ASYNC===>"+stickyMessage.getMsg());
}

}

三、界面
EventBus使用步骤(二):黏性事件(每天学一点)_第1张图片

首先在未订阅的情况下点击发送按钮发送一个黏性事件,然后点击订阅,Log日志打印结果如下:

09-24 23:36:32.470 10514-10514/com.shangyu.eventbusdemo I/ThreadMode.MAIN: ThreadMode.MAIN===>0
09-24 23:36:32.470 10514-10514/com.shangyu.eventbusdemo I/ThreadMode.POSTING: ThreadMode.POSTING===>0
09-24 23:36:32.470 10514-10621/com.shangyu.eventbusdemo I/ThreadMode.ASYNC: ThreadMode.ASYNC===>0
09-24 23:36:32.470 10514-10622/com.shangyu.eventbusdemo I/ThreadMode.BACKGROUND: ThreadMode.BACKGROUND===>0

然后取消订阅,再次点击5次发送消息按钮,
然后点击订阅,Log日志打印结果如下:

09-24 23:36:32.470 10514-10514/com.shangyu.eventbusdemo I/ThreadMode.MAIN: ThreadMode.MAIN===>6
09-24 23:36:32.470 10514-10514/com.shangyu.eventbusdemo I/ThreadMode.POSTING: ThreadMode.POSTING===>6
09-24 23:36:32.470 10514-10621/com.shangyu.eventbusdemo I/ThreadMode.ASYNC: ThreadMode.ASYNC===>6
09-24 23:36:32.470 10514-10622/com.shangyu.eventbusdemo I/ThreadMode.BACKGROUND: ThreadMode.BACKGROUND===>6

好了就这一条,完毕。

你可能感兴趣的:(android学习)