简体   繁体   English

Android Oreo上的广播接收器

[英]Broadcast Receiver on Android Oreo

我有一个网络更改接收器类,它扩展到广播接收器,但我不太确定它在android Oreo中工作,Oreo是否支持广播接收器,如果它不支持,那么另一种方法是什么

Oreo supports Broadcast Receivers but with some restrictions on Implicit broadcast that are declared in manifest. Oreo支持广播接收器,但对清单中声明的​​隐式广播一些限制。

Implicit vs Explicit Broadcast: 隐式与显式广播:

According to the documentation , 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. 例如, ACTION_PACKAGE_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. 但是, ACTION_MY_PACKAGE_REPLACED不是隐式广播,因为它只发送到其软件包被替换的应用程序,无论有多少其他应用程序为该广播注册了监听器。

So any broadcast receivers that we have defined statically within our application manifest that are listening for implicit broadcasts will no longer receive those broadcasts. 因此,我们在应用程序清单中静态定义的正在侦听隐式广播的任何广播接收器将不再接收这些广播。

The reason for this change is that implicit broadcasts would previously trigger any component that was listening for them within the manifest— this could have an adverse effect on application and device performance due to large numbers of applications registered to receive specific broadcasts all being triggered at the same time. 这种变化的原因是隐式广播之前会触发在清单中监听它们的任何组件 - 这可能会对应用程序和设备性能产生负面影响,因为大量应用程序注册接收特定广播都会在同时。

But there is a list of exceptions when it comes to implicit broadcasts — this means that there are still some which you can register to receive broadcasts for. 但是当涉及隐式广播时,有一个例外列表 - 这意味着仍然有一些你可以注册接收广播。 They are all listed below: 它们都列在下面:

在此输入图像描述

So if the broadcast that you have registered receivers for is on this list, then it will still function as it did previously. 因此,如果您已注册接收器的广播在此列表中,那么它仍将像以前一样运行。 However, if the broadcast that you have registered to receive is not on this list then you should use some alternative solution like: 但是,如果您已注册接收的广播不在此列表中,那么您应该使用一些替代解决方案,例如:

  • Create the receiver at runtime by calling Context.registerReceiver() , instead of declaring the receiver in the manifest. 通过调用Context.registerReceiver()在运行时创建接收器,而不是在清单中声明接收器。
  • Use a scheduled job to check for the condition that would have triggered the implicit broadcast. 使用预定作业来检查可能触发隐式广播的条件。

For more information 欲获得更多信息

It's not supported in Oreo as manifest tag, you must have to register it at an Service/ Activity with context.registerReceiver(). 在Oreo中不支持它作为清单标记,您必须使用context.registerReceiver()在Service / Activity中注册它。 Or you use the WorkManager to schedule something for specific network conditions. 或者您使用WorkManager为特定网络条件安排某些事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM