有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java[Android]Android系统gerrentee是否根据用户定义的广播接收器的安卓:优先级来决定其执行顺序?

我设计了一个包含服务的组件(打包在.jar文件中),这个组件可以包含在许多应用程序中(比如说,可能有许多应用程序中都有这个组件,安装在一台设备上)。但一台设备上预计只有一个组件实例,这意味着如果“A”应用程序中的组件已启动,“B”应用程序中的组件将不会在“A”的生命周期中启动

也许有一天,该设备上安装了另一个新的应用程序,但该组件有了新版本。我想要的是确保启动的组件始终是最新版本

所以,我想使用BroadcastReceiver和安卓:priority作为解决方案(新版本将具有更高的优先级)。但我必须确定Android系统是否会根据其优先级调整执行顺序(BroadcastReceiver.onReceive)

有人给我一个提示吗?那将是非常感激的:)


共 (1) 个答案

  1. # 1 楼答案

    简言之:如果你使用有序广播,是的

    根据Android doc

    Ordered broadcasts (sent with Context.sendOrderedBroadcast) are delivered 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.

    因此,您必须定义android:priority,并且必须使用以下命令发送有序广播:Context.sendOrderedBroadcast

    请注意,您的一个接收器甚至可以中止broacast,这样它就不会传播到下一个接收器。(见:abortBroadcast