且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

广播接收器无法在Android Oreo中使用

更新时间:2023-11-16 19:03:22

广播限制

如果某个应用程序注册接收广播,则每次发送广播时,该应用程序的接收器都会消耗资源.如果太多的应用程序注册为无法根据系统事件接收广播,则可能会导致问题.触发广播的系统事件可能导致所有这些应用连续消耗资源,从而损害用户体验.为了缓解此问题,Android 7.0(API级别25)对广播设置了限制,如背景优化中所述. Android 8.0(API级别26)使这些限制更加严格.

If an app registers to receive broadcasts, the app's receiver consumes resources every time the broadcast is sent. This can cause problems if too many apps register to receive broadcasts based on system events; a system event that triggers a broadcast can cause all of those apps to consume resources in rapid succession, impairing the user experience. To mitigate this problem, Android 7.0 (API level 25) placed limitations on broadcasts, as described in Background Optimization. Android 8.0 (API level 26) makes these limitations more stringent.

  1. 面向Android 8.0或更高版本的应用无法再在其清单中为隐式广播注册广播接收器.隐式广播是不专门针对该应用的广播.例如,ACTION_PACKAGE_REPLACED是隐式广播,因为它已发送给所有注册的侦听器,让他们知道设备上的某些程序包已被替换.但是,ACTION_MY_PACKAGE_REPLACED并不是隐式广播,因为它仅发送到替换了包的应用程序,无论有多少其他应用程序已为该广播注册了侦听器.

  1. Apps that target Android 8.0 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest. An implicit broadcast is a broadcast that does not target that app specifically. For example, ACTION_PACKAGE_REPLACED is an implicit broadcast, since it is sent to all registered listeners, letting them know that some package on the device was replaced. However, ACTION_MY_PACKAGE_REPLACED is not an implicit broadcast, since it is sent only to the app whose package was replaced, no matter how many other apps have registered listeners for that broadcast.

应用程序可以继续在清单中注册显式广播.

Apps can continue to register for explicit broadcasts in their manifests.

应用程序可以在运行时使用Context.registerReceiver()为任何广播(隐式或显式)注册接收器.

Apps can use Context.registerReceiver() at runtime to register a receiver for any broadcast, whether implicit or explicit.

需要签名许可的广播不受此限制,因为这些广播仅发送到使用相同证书签名的应用,而不发送到设备上的所有应用.

Broadcasts that require a signature permission are exempted from this restriction, since these broadcasts are only sent to apps that are signed with the same certificate, not to all the apps on the device.

在此处查看官方文档

希望这会有所帮助.