EventBus之EventBusBuilder

Configuration

EventBusBuilder类配置EventBus的各个方面。例如,下面是如何构建一个EventBus,在发布的事件没有订阅者时保持安静:

EventBus eventBus = EventBus.builder()
    .logNoSubscriberMessages(false)
    .sendNoSubscriberEvent(false)
    .build();

另一个例子是订阅者抛出异常时失败:

EventBus eventBus = EventBus.builder().throwSubscriberException(true).build();

注意:默认情况下,EventBus捕获(catch)从订阅者方法抛出的异常,并发送一个SubscriberExceptionEvent,该事件可能被处理,但不一定必须被处理。

Configure the default EventBus instance

Configure the default EventBus instance

使用EventBus.getDefault()是一种从应用程序的任何位置获取共享EventBus实例的简单方法。EventBusBuilder还允许使用installDefaultEventBus()方法配置这个默认实例。

例如,可以配置默认EventBus实例来重新抛出异常,这些异常发生在订阅方方法中。但我们只在调试构建时才这样做,因为这可能会在异常情况下导致应用程序崩溃:

EventBus.builder().throwSubscriberException(BuildConfig.DEBUG).installDefaultEventBus();

注意:在第一次使用默认EventBus实例之前,只能执行一次。对installDefaultEventBus()的后续调用将引发异常。这确保了应用程序中的行为一致。应用程序类是在使用默认EventBus实例之前配置它的好地方。

EventBusBuilder:

EventBusBuilderaddIndex(SubscriberInfoIndex index)

Adds an index generated by EventBus' annotation preprocessor.

添加由EventBus的注释预处理器生成的索引。

EventBusbuild()

Builds an EventBus based on the current configuration.

EventBusBuildereventInheritance(boolean eventInheritance)

By default, EventBus considers the event class hierarchy (subscribers to super classes will be notified).

默认情况下,EventBus考虑事件类层次结构(将通知超类的订阅者)。

EventBusBuilderexecutorService(java.util.concurrent.ExecutorService executorService)

Provide a custom thread pool to EventBus used for async and background event delivery.

为EventBus提供一个自定义线程池,用于异步和后台事件交付。

EventBusBuilderignoreGeneratedIndex(boolean ignoreGeneratedIndex)

Forces the use of reflection even if there's a generated index (default: false).

强制使用反射,即使生成了索引(默认值:false)。

EventBusinstallDefaultEventBus()

Installs the default EventBus returned by EventBus.getDefault() using this builders' values.

使用此构造器的值安装 EventBus. getdefault() 返回的默认EventBus。

必须在第一次使用默认EventBus之前只执行一次。

抛出:EventBusException——如果已经有一个默认的EventBus实例

EventBusBuilderlogNoSubscriberMessages(boolean logNoSubscriberMessages)

Default: true

EventBusBuilderlogSubscriberExceptions(boolean logSubscriberExceptions)

Default: true

EventBusBuildersendNoSubscriberEvent(boolean sendNoSubscriberEvent)

Default: true

EventBusBuildersendSubscriberExceptionEvent(boolean sendSubscriberExceptionEvent)

Default: true

EventBusBuilderskipMethodVerificationFor(java.lang.Class<?> clazz)

Method name verification is done for methods starting with onEvent to avoid typos; using this method you can exclude subscriber classes from this check.

对以onEvent开头的方法进行方法名验证,避免输入错误;使用此方法,您可以将订阅程序类排除在此检查之外。

还禁用方法修饰符的检查(公共的,不是静态的,也不是抽象的)。

EventBusBuilderstrictMethodVerification(boolean strictMethodVerification)

Enables strict method verification (default: false).

启用严格的方法验证(默认值:false)。

EventBusBuilderthrowSubscriberException(boolean throwSubscriberException)

Fails if an subscriber throws an exception (default: false).

如果订阅者抛出异常(默认:false),则失败。

提示:将其与BuildConfig一起使用。调试使应用程序在调试模式下崩溃(仅限)。这样,您就不会错过开发过程中的异常。

GitHub地址

由于作者水平有限,语言描述及代码实现中难免有纰漏,望各位看官多提宝贵意见!

Hello , World !

感谢所有!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
EventBus 是 Android 开发中一款非常实用的事件发布/订阅框架,它可以帮助开发者更方便地进行组件间通信,减少代码耦合度,提高代码复用性。在使用 EventBus 过程中,回调函数是非常重要的组成部分,下面我们来一起学习一下如何使用 EventBus 进行回调。 首先,在使用 EventBus 进行回调之前,需要先进行 EventBus 的注册和注销操作。在 Activity 或 Fragment 中注册 EventBus 的代码如下: ```java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 注册 EventBus EventBus.getDefault().register(this); } ``` 在 Activity 或 Fragment 销毁时,需要进行注销操作,代码如下: ```java @Override protected void onDestroy() { super.onDestroy(); // 注销 EventBus EventBus.getDefault().unregister(this); } ``` 接下来,我们来看一下如何使用 EventBus 进行回调。假设我们有一个 Activity A,需要从另一个 Activity B 中获取数据并进行处理,在 Activity A 中,我们可以定义一个事件,如下: ```java public class DataEvent { private String data; public DataEvent(String data) { this.data = data; } public String getData() { return data; } } ``` 在 Activity B 中,我们可以通过 EventBus 发送该事件,并传递数据,如下: ```java EventBus.getDefault().post(new DataEvent("Hello EventBus!")); ``` 在 Activity A 中,我们需要定义一个方法来接收该事件,并进行相应的处理,代码如下: ```java @Subscribe(threadMode = ThreadMode.MAIN) public void onDataEvent(DataEvent event) { String data = event.getData(); // 处理数据 } ``` 在这个方法上使用了 @Subscribe 注解,表示该方法用来接收事件,threadMode = ThreadMode.MAIN 表示该方法在主线程中执行。当 Activity A 接收到事件后,该方法就会被调用,并将事件中传递的数据作为参数传递给该方法。这样,我们就实现了通过 EventBus 进行回调的功能。 需要注意的是,使用 EventBus 进行回调时,需要保证订阅者和发布者在同一进程中,否则无法接收到事件。另外,在使用 EventBus 进行回调时,建议使用事件模型,将数据封装成事件对象,这样可以更好地进行解耦和管理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

windfallsheng

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值