android事件总线EventBus3.0使用方法详解
作者:c_bulrush 发布时间:2022-02-17 16:30:41
一.EventBus概述
1.EventBus的三要素
EventBus有三个主要的元素需要我们先了解一下:
Event:事件,可以是任意类型的对象。
Subscriber:事件订阅者,在EventBus3.0之前消息处理的方法只能限定于onEvent、onEventMainThread、onEventBackgroundThread和onEventAsync,他们分别代表四种线程模型。而在EventBus3.0之后,事件处理的方法可以随便取名,但是需要添加一个注解@Subscribe,并且要指定线程模型(默认为POSTING),四种线程模型下面会讲到。
Publisher:事件发布者,可以在任意线程任意位置发送事件,直接调用EventBus的post(Object)方法。可以自己实例化EventBus对象,但一般使用EventBus.getDefault()就好了,根据post函数参数的类型,会自动调用订阅相应类型事件的函数。
2.EventBus的四种ThreadMode(线程模型)
EventBus3.0有以下四种ThreadMode:
POSTING(默认):如果使用事件处理函数指定了线程模型为POSTING,那么该事件在哪个线程发布出来的,事件处理函数就会在这个线程中运行,也就是说发布事件和接收事件在同一个线程。在线程模型为POSTING的事件处理函数中尽量避免执行耗时操作,因为它会阻塞事件的传递,甚至有可能会引起ANR。
MAIN:事件的处理会在UI线程中执行。事件处理时间不能太长,长了会ANR的。
BACKGROUND:如果事件是在UI线程中发布出来的,那么该事件处理函数就会在新的线程中运行,如果事件本来就是子线程中发布出来的,那么该事件处理函数直接在发布事件的线程中执行。在此事件处理函数中禁止进行UI更新操作。
ASYNC:无论事件在哪个线程发布,该事件处理函数都会在新建的子线程中执行,同样,此事件处理函数中禁止进行UI更新操作。
二.EventBus的基本用法
1.自定义一个事件类(相当于我们平常所用的bean类)
public class MessageEvent {
...
}
2.在需要订阅的地方注册
EventBus.getDefault().register(this);
3.发送事件
第一种.普通事件
EventBus.getDefault().post(messageEvent);
第二种.粘性事件
EventBus.getDefault().postSticky(messageEvent);
4.处理事件(eg.刷新UI)
@Subscribe(threadMode = ThreadMode.MAIN)
public void XXX(MessageEvent messageEvent) {
...
}
5.取消事件订阅
@Override
protected void onDestroy() {
EventBus.getDefault().unregister(this);
super.onDestroy();
}
三.EventBus的实际应用(模拟登陆传值)
1.导入3.0依赖
compile 'org.greenrobot:eventbus:3.0.0'
2.定义消息事件类
public class MessageEvent {
public final String uname;
public final String upass;
public MessageEvent(String name,String pass) {
this.uname = name;
this.upass = pass;
}
}
3.发送事件(粘性事件)
public class MainActivity extends AppCompatActivity {
private String username;
private String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextInputLayout usernameWrapper = (TextInputLayout) findViewById(R.id.usernameWrapper);
final TextInputLayout passwordWrapper = (TextInputLayout) findViewById(R.id.passwordWrapper);
Button btn = (Button) findViewById(R.id.btn);
usernameWrapper.setHint("请输入账号");
passwordWrapper.setHint("请输入密码");
//点击事件
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hideKeyboard();
username = usernameWrapper.getEditText().getText().toString();
password = passwordWrapper.getEditText().getText().toString();
if (!validateEmail(username)) {
usernameWrapper.setError("Not a valid email address!");
} else if (!validatePassword(password)) {
passwordWrapper.setError("Not a valid password!");
} else {
usernameWrapper.setErrorEnabled(false);
passwordWrapper.setErrorEnabled(false);
//发送粘性事件//////////////////
EventBus.getDefault().postSticky(new MessageEvent(username,password));
startActivity(new Intent(MainActivity.this,SecondActivity.class));
}
}
});
}
private void hideKeyboard() {
View view = getCurrentFocus();
if (view != null) {
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).
hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
//邮箱验证
public boolean validateEmail(String email) {
return email.length() > 5;
}
// 密码验证
public boolean validatePassword(String password) {
return password.length() > 5;
}
}
4.注册和取消订阅事件
public class SecondActivity extends AppCompatActivity {
private TextView name,pass;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
name= (TextView) findViewById(R.id.uname);
pass= (TextView) findViewById(R.id.upass);
button= (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//注册EventBus
EventBus.getDefault().register(SecondActivity.this);
}
});
}
//事件订阅者处理事件
@Subscribe(threadMode = ThreadMode.POSTING,sticky = true)
public void onUserEvent(MessageEvent event) {
name.setText("用户名:" + event.uname);
pass.setText("用户名:" + event.upass);
}
//取消注册
@Override
protected void onDestroy() {
EventBus.getDefault().unregister(this);
super.onDestroy();
}
}
布局
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="Welcome"
android:textSize="30sp"
android:textColor="#333333"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:id="@+id/usernameWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Username"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/passwordWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/usernameWrapper"
android:layout_marginTop="4dp">
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Password"/>
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/btn"
android:layout_marginTop="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"/>
</LinearLayout>
</LinearLayout>
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center">
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="接收数据" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/uname"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/upass"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
来源:http://blog.csdn.net/c_bulrush/article/details/78467104


猜你喜欢
- 本文实例分析了Android中GridView和ArrayAdapter用法。分享给大家供大家参考,具体如下:GridView是一个表格化的
- AnDroidDraw 是一个与 DroidDraw 集成的 Android 应用程序,它允许你从 DroidDraw 应用 程序下载你的
- SpringMvc中普通类注入Service为null场景:使用Quartz定时器时,普通的java类需要注入spring的service类
- 背景#目前我主要负责的一个项目是一个 C/S 架构的客户端开发,前端主要是通过 WPF 相关技术来实现,后端是通过 Python 来实现,前
- C#貌似没有专门用于ASCII码转字符或字符转ASCII码的系统函数,所以小编这里就借用一下强制类型转换来实现ASCII码与字符之间的互转。
- 什么是进程?当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源。而一个进程又是由多个线程所组成的。什么
- RESTful 一种软件架构风格,设计风格而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设
- 一.使用场景一次请求需要往数据库插入多条数据时,可以节省大量时间,mysql操作在连接和断开时的开销超过本次操作总开销的40%。二.实现方法
- 限流器算法目前常用限流器算法为两种:令牌桶算法和漏桶算法,主要区别在于:漏桶算法能够强行限制请求速率,平滑突发请求,而令牌桶算法在限定平均速
- 表述在一次服务更新后发现每天凌晨0点3秒服务准时挂,开始的时候认为是maven依赖中存在system.exit(3)类似这样的代码,但是我想
- Java反射机制在Spring IOC的应用IOC:即“控制反转”,不是什么技术,而是一种思想。使用IOC意味着将你设计好的对象交给容器控制
- 最近在做数据挖掘的课程设计,需要将数据分析的结果很直观的展现给用户,这就要用到数据统计图,要实现这个功能就需要几个第三方包了:1.
- 一、类成员的访问级别public:可由任何代码访问。private(默认):只能由类中的代码访问。internal:只能由它所在的项目(程序
- 关键要点可变模型应该具备自我验证的能力,并实现验证接口。在共享对象时(特别是在跨线程共享时),考虑使用不可变模型。考虑支持MVVM风格UI的
- 1.非静态成员变量当成员变量为非静态成员变量且对当前类进行实例化时,将会产生死循环例子:public class ConstructorCl
- 这篇文章主要介绍了SpringBoot Jpa分页查询配置方式解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价
- 1、在pom.xml文件引入依赖<!-- 运行状态监控actuator依赖 --> <depen
- 在定义API的时候,对于一些返回集合对象的方法,很多人喜欢将返回类型定义成IEnumerable<T>,这本没有什么问题。这里要
- 需求是要做几个小游戏的抽奖功能,需要根据不同的游戏有不同的抽奖规则,其中也有很多共性,可归纳为只按奖品占比抽取、奖品占比与奖品数量抽取、分段
- 大部分Java开发者都在使用Map,特别是HashMap。HashMap是一种简单但强大的方式去存储和获取数据。但有多少开发者知道HashM