浅聊BroadcastReceiver与EventBus的事件传递操作

版权声明:天际流火叩响大地之门,岁月星辰刻画沧桑年轮! https://blog.csdn.net/ytfunnysite/article/details/80691128

截止文章发稿时,EventBus已经更新到了3.1.1

implementation ‘org.greenrobot:eventbus:3.1.1’

BroadcastReceiver使用:

定义一个广播接收器

在需要接受事件消息的地方定义一个广播接收器

public class MessageBroadcastReceiver extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {
                buttonbus.setText("Message from SecondActivity:" + intent.getStringExtra("message"));

    }

onCreat()中注册广播

在需要接收广播的acvitity中注册广播

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        //注册广播 
        IntentFilter intentFilter = new IntentFilter("message_broadcast");
        mBroadcastReceiver = new MessageBroadcastReceiver();
        registerReceiver(mBroadcastReceiver, intentFilter);


    }

需要发送广播的地方定义发送方法

  public void send(View view) {
        Intent intent = new Intent();
        intent.setAction("message_broadcast");
        intent.putExtra("message", "广播发送");
        sendBroadcast(intent);
        finish();
    }

广播中耗时操作处理

迷之缩进

   //    定义一个广播接收器:
        public class MessageBroadcastReceiver extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //从服务器上获取数据 ......
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //将获取的数据展示在界面上 ......
                            }
                        });
                    }
                }).start();
            }

解除注册

   @Override
        protected void onDestroy () {
            super.onDestroy();
           //取消广播注册
            unregisterReceiver(mBroadcastReceiver);

        }

EventBus处理方案:

接受信息的地方onCreate中进行注册

 EventBus.getDefault().register(this);

定义消息处理方法(包括耗时操作的方法)

  //定义一个消息处理的方法:
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onShowMessage(MessageEvent messageEvent) {
        buttonbus.setText(messageEvent.getMessage());
    }

    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    public void onGetDataEvent(MessageEvent messageEvent) {
//        从服务器上获取数据 ......
        EventBus.getDefault().post(new ShowMessageEvent());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onShowDataEvent(ShowMessageEvent showMessageEvent) {
        //将获取的数据展示在界面上 ......

    }

发送普通消息的方法

 public void send(View view) {
    EventBus.getDefault().post(new MessageEvent("EventBus消息"));
        }

发送黏性消息

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        ButterKnife.bind(this);
//        黏性时间,收到注册之前发送的最新一次消息
        getSticky();

    }

    private void getSticky() {
        findViewById(R.id.post).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                EventBus.getDefault().postSticky(new MessageEvent("test" + index++));
            }
        });
        findViewById(R.id.regist).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().register(SecondActivity.this);
            }
        });
        findViewById(R.id.unregist).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().unregister(SecondActivity.this);
            }
        });

    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onMessageEventPostThread(MessageEvent messageEvent) {
        Log.e("PostThread", messageEvent.getMessage());
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessageEventMainThread(MessageEvent messageEvent) {
        Log.e("MainThread", messageEvent.getMessage());
    }

    @Subscribe(threadMode = ThreadMode.BACKGROUND, sticky = true)
    public void onMessageEventBackgroundThread(MessageEvent messageEvent) {
        Log.e("BackgroundThread", messageEvent.getMessage());
    }

    @Subscribe(threadMode = ThreadMode.ASYNC, sticky = true)
    public void onMessageEventAsync(MessageEvent messageEvent) {
        Log.e("Async", messageEvent.getMessage());
    }


    public void actionIntent(Context context) {
        Intent intent = new Intent(context, SecondActivity.class);
        startActivity(intent);
    }

解除注册

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

        }
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页