EventBus的基本使用

本文详细介绍EventBus的基本使用方法,包括如何在不同界面间传递消息、构造消息类、发布及接收消息等。同时,还介绍了粘性事件的应用场景及实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    这几天有时间整理一下eventbus的使用方法。

使用步骤:A跳转到B , B发消息A接收。

1.添加jar到libs文件加下

2.注册EventBus.getDefault().register(this);

3.解注册EventBus.getDefault().unregister(this);(在注册的时候记得解注册)

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

4.构造发送类

	public class AnyEventBus {
    	private String mMsg;
    	public AnyEventBus(String msg){
       		mMsg = msg;
    	}
    	public String getMsg(){
        	return mMsg;
    	}
	}

5.发布消息

	EventBus.getDefault().post(new AnyEventBus("123"));

6.接收消息()

ThreadMode.MAIN 表示这个方法在主线程中执行

ThreadMode.BACKGROUND 表示该方法在后台执行,不能并发处理

ThreadMode.ASYNC 也表示在后台执行,可以异步并发处理。

ThreadMode.POSTING 表示该方法和消息发送方在同一个线程中执行

@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(AnyEventBus event) {
    String a = event.getMsg();
    tv.setText(a);
}
完整代码:

A界面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.mianshi.haohankwji.meventbus.MainActivity">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="A界面"
    android:id="@+id/textView" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳转到B界面"
        android:id="@+id/button"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="33dp" />
</RelativeLayout>

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    EventBus.getDefault().register(MainActivity.this);//注册eventbus
    button = (Button) findViewById(R.id.button);
    tv = (TextView) findViewById(R.id.tv);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, FirstActivity.class);
            startActivity(intent);
        }
    });

}

/**
 * 接受消息
 * @param event
 */
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(AnyEventBus event) {
    String a = event.getMsg();
    tv.setText(a);
}

@Override
protected void onDestroy() {//解注册
    super.onDestroy();
    EventBus.getDefault().unregister(MainActivity.this);
}

B界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="B界面"/>
    <Button
        android:id="@+id/button_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="给上A界面发送“123" />

    <Button
        android:id="@+id/button_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳到C界面并发送”456" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv"/>
</LinearLayout>

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first_event);
    button_1 = (Button) findViewById(R.id.button_1);
    button_2 = (Button) findViewById(R.id.button_2);
    tv = (TextView) findViewById(R.id.tv);
    button_1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EventBus.getDefault().post(new AnyEventBus("123"));
            finish();

        }
    });
}
public void onEventMainThread(AnyEventBus event) {
    String a = event.getMsg();
    tv.setText(a);
}


消息类
public class AnyEventBus {
    private String mMsg;
    public AnyEventBus(String msg){
        mMsg = msg;
    }
    public String getMsg(){
        return mMsg;
    }
}

粘性事件:B界面跳转到C界面并传递数据

1.构造信息发送类

2.发布消息EventBus.getDefault().postSticky(new StickyEvent("456"));

3.接收消息

4.注册

5.解注册

完整代码:

消息类
public class StickyEvent {
    public String msg;
    public StickyEvent(String msg) {
        this.msg = msg;
    }

}

B界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="B界面"/>
    <Button
        android:id="@+id/button_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="给上A界面发送“123" />

    <Button
        android:id="@+id/button_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳到C界面并发送”456" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv"/>
</LinearLayout>

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first_event);
    button_1 = (Button) findViewById(R.id.button_1);
    button_2 = (Button) findViewById(R.id.button_2);
    tv = (TextView) findViewById(R.id.tv);
    button_2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EventBus.getDefault().postSticky(new StickyEvent("456"));
            Intent intent = new Intent(FirstActivity.this,SecandActivity.class);
            startActivity(intent);
        }
    });
}
public void onEventMainThread(AnyEventBus event) {
    String a = event.getMsg();
    tv.setText(a);
}

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="C界面" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="接收" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_secand_event);
    tv = (TextView) findViewById(R.id.tv);
    button2 = (Button) findViewById(R.id.button2);
    initciew();
}

private void initciew() {
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (sign){
                sign = false;//注册一次就可以,避免点击一次注册一次
                EventBus.getDefault().register(SecandActivity.this);//注册eventbus
            }
        }
    });
}

@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void onEvent(StickyEvent event) {

    String a = event.msg;
    tv.setText(a);
}

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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大不懂

码字不易,一块也是爱,么么

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值