且构网

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

广播接收器在android oreo中不起作用

更新时间:2023-11-16 19:07:46

广播限制

如果应用注册接收广播,则每次发送广播时应用的接收器都会消耗资源.如果太多的应用程序注册接收基于系统事件的广播,这可能会导致问题;触发广播的系统事件可能导致所有这些应用程序快速连续消耗资源,从而损害用户体验.为缓解此问题,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.

在此处查看官方文档

希望这会有所帮助.