使用意图或事件总线在同一应用程序进行通信总线、意图、应用程序、事件

2023-09-05 00:31:12 作者:精彩小伙、文韬武略

我知道如何使用意图与系统/其他应用程序进行通信。我知道如何在同一应用程序中使用意图。我也明白如何使用奥托在同一应用程序内进行通信。

I understand how to use Intents to communicate with the system/other apps. I understand how to use Intents within the same App. I also understand how to use Otto to communicate within the same App.

什么是使用奥托与意图我的活动之间进行通信的专业/反对/服务?

What are the Pro/Cons of using Otto vs. Intents to communicate between my Activities/Services?

推荐答案

赞成使用奥托:

您去设计自己的事件类型,对必须使用自定义操作什么的来区分一个意图从另一个

一切都在自己的进程 startActivity(对比度()和亲属,其中的总是的包括IPC,即使活动你开始在自己的应用程序),速度和安全性

Everything is within your own process (contrast with startActivity() and kin, which always involve IPC, even if the activity you are starting is in your own app), for speed and security

一个少一点的编码,因为你不必实例化的IntentFilter 的BroadcastReceiver 对象

A bit less coding, as you aren't having to instantiate IntentFilter or BroadcastReceiver objects

它提供了生产模式(作为一个准替代持久广播)

It offers the producer pattern (as a quasi-replacement for sticky broadcasts)

作为操作系统的一部分,则它有可能被更频繁地更新

Being not part of the OS, it has the potential to be updated more frequently

缺点使用奥托:

它无法启动的活动

It cannot start an activity

它无法启动服务

它不能绑定到一个服务

它不能发送广播

它不能在使用 PendingIntent 或任何真正的IPC

It cannot be used in a PendingIntent or for any true IPC

IOW,奥托真正的比较与 LocalBroadcastManager ,不能与一般使用意图的。

IOW, the true comparison for Otto is with LocalBroadcastManager, not with the general use of Intents.