尚硅谷_EventBus

EventBus是一种用于Android的事件发布-订阅总线,由GreenRobot开发,Gihub地址是:EventBus。它简化了应用程序内各个组件之间进行通信的复杂度,尤其是碎片之间进行通信的问题,可以避免由于使用广播通信而带来的诸多不便。

1.2 四种线程模型

EventBus3.0有四种线程模型,分别是:

  1. POSTING:默认,表示事件处理函数的线程跟发布事件的线程在同一个线程。
  2. MAIN:表示事件处理函数的线程在主线程(UI)线程,因此在这里不能进行耗时操作。
  3. BACKGROUND:表示事件处理函数的线程在后台线程,因此不能进行UI操作。如果发布事件的线程是主线程(UI线程),那么事件处理函数将会开启一个后台线程,如果果发布事件的线程是在后台线程,那么事件处理函数就使用该线程。
  4. ASYNC:表示无论事件发布的线程是哪一个,事件处理函数始终会新建一个子线程运行,同样不能进行UI操作。

EventBus 使用

引入依赖

在使用之前先要引入如下依赖:

implementation 'org.greenrobot:eventbus:3.1.1'

 

 

  

 

EventBusActivity.java
package com.example.testapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.testapplication.event.MessageEvent;
import com.example.testapplication.event.StickyEvent;

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

public class EventBusActivity extends AppCompatActivity {
    private TextView tv_title;
    private Button bt_eventbus_send;
    private Button bt_eventbus_sticky;
    private TextView tv_eventbus_result;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eventbus);

        initView(); // 初始化
        tv_title.setText("EventBus");

        initListener();  //点击事件

        // 1.注册广播
        EventBus.getDefault().register(EventBusActivity.this);

    }

    private void initListener() {
        // 跳转到发送页面
        bt_eventbus_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(EventBusActivity.this,EventBusSendActivity.class);
                startActivity(intent);

            }
        });

        // 发送粘性事件到发送页面
        bt_eventbus_sticky.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 粘性事件2.发送粘性事件
                EventBus.getDefault().postSticky(new StickyEvent("我是粘性事件"));
                Intent intent = new Intent(EventBusActivity.this,EventBusSendActivity.class);
                startActivity(intent);


            }
        });

    }
    private void initView() {
        tv_title = (TextView)findViewById(R.id.tv_title);

        bt_eventbus_send = (Button)findViewById(R.id.bt_eventbus_send);
        bt_eventbus_sticky = (Button)findViewById(R.id.bt_eventbus_sticky);
        tv_eventbus_result = (TextView)findViewById(R.id.tv_eventbus_result);
    }

    // 5.接收消息
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void MessageEventBut(MessageEvent event){
        // 显示接收的消息
        tv_eventbus_result.setText(event.name);
    }
/**
 * 在Activity的生命周期中,onDestory()方法是他生命的最后一步,
 * 资源空间等就被回收了。当重新进入此Activity的时候,
 * 必须重新创建,执行onCreate()方法。
 *
 * */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 2.解注册
        EventBus.getDefault().unregister(EventBusActivity.this);
    }
}
EventBusSendActivity.java
package com.example.testapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.testapplication.event.MessageEvent;
import com.example.testapplication.event.StickyEvent;

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

// eventbus的发送数据页面
public class EventBusSendActivity extends AppCompatActivity {
    private TextView tv_title;
    private Button bt_eventbus_send_main;
    private Button bt_eventbus_send_sticky;
    private TextView tv_eventbus_send_result;
    boolean isFirstFlag = true;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_busend);
        tv_title = (TextView)findViewById(R.id.tv_title);
        bt_eventbus_send_main = (Button)findViewById(R.id.bt_eventbus_send_main);
        bt_eventbus_send_sticky = (Button)findViewById(R.id.bt_eventbus_send_sticky);
        tv_eventbus_send_result = (TextView)findViewById(R.id.tv_eventbus_send_result);
        tv_title.setText("EventBus发送数据页面");


        initListener(); // 监听事件

    }

    private void initListener() {
        // 主线程发送数据按钮点击事件处理
        bt_eventbus_send_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 4.发送消息
                EventBus.getDefault().post(new MessageEvent("主线程发送过来的数据"));
                // 结束当前页面
                finish();


            }
        });

        // 接收粘性事件数据的点击事件处理
        bt_eventbus_send_sticky.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 粘性事件4
                if(isFirstFlag){
                    isFirstFlag = false;

                    // 粘性事件4.注册
                    EventBus.getDefault().register(EventBusSendActivity.this);

                }

            }
        });

    }
    // 粘性事件3 接收粘性事件
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void StickyEventBus(StickyEvent event){
        // 显示接收数据
        tv_eventbus_send_result.setText(event.msg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 粘性事件5 解注册
        EventBus.getDefault().removeAllStickyEvents();
        EventBus.getDefault().unregister(EventBusSendActivity.this);

    }
}
MessageEvent.java
package com.example.testapplication.event;
// 3.创建发送消息类
public class MessageEvent {
    public String name;
    public MessageEvent(String name){
        this.name = name;
    }
}
StickyEvent.java
package com.example.testapplication.event;
// 粘性事件1.创建一个粘性事件类
public class StickyEvent {
    public String msg;
    public StickyEvent(String msg){
        this.msg = msg;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <include layout="@layout/titlebar"></include>

    <Button
        android:id="@+id/bt_eventbus_send"
        android:text="跳转到发送页面"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/bt_eventbus_sticky"
        android:text="发送粘性事件跳转到发送页面"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:text="接收显示的结果"
        android:textSize="20sp"
        android:layout_margin="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_eventbus_result"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <include layout="@layout/titlebar"></include>

    <Button
        android:id="@+id/bt_eventbus_send_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="主线程发送数据" />

    <Button
        android:id="@+id/bt_eventbus_send_sticky"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="接收粘性数据" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:text="显示结果:"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_eventbus_send_result"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值