广播接收器如何在活动之间工作?

How does a broadcast receiver work among activities?

我有多个活动,假设 A、B、C、D 都注册到同一个 BroadCastReceiver。在发送广播消息时,活动将按哪个顺序接收广播意图。是像 A , B , C , D 这样的顺序还是随机的。

请告诉我,给我一些建议。

您无法控制它,因为系统将这些操作分派给已注册的广播侦听器。您无法保证将触发广播听众的顺序。

除非是命令广播。来自 docs:

The sendOrderedBroadcast(Intent, String) method sends broadcasts to one receiver at a time. As each receiver executes in turn, it can propagate a result to the next receiver, or it can completely abort the broadcast so that it won't be passed to other receivers. The order receivers run in can be controlled with the android:priority attribute of the matching intent-filter; receivers with the same priority will be run in an arbitrary order.

有可能已经订购了广播。那要看你是不是主播了。如果您正在广播,请使用 sendOrderedBroadcast(Intent, String) 并优先考虑您的接收者。如果其他人是广播者,您必须知道他是否按顺序发送。如果他没有,你就没有机会了,否则你可以优先考虑你的接收者。

在android中可以通过三种方式发送Broadcast。即1.sendBroadcast 2.sendOrderedBroadcast, 3.sendStickyBroadcast。这里你说的是 sendBroadcast 所以它会简单地将广播发送给注册的广播接收器,而不管广播是以什么顺序注册的。