26及更高版本上使用

26及更高版本上使用

本文介绍了LocationManager.PROVIDERS_CHANGED_ACTION将无法在API 26及更高版本上使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码获取位置开/关事件.

I am using following code to get location on/off event.

<receiver
    android:name=".receivers.GpsReceiver"
    android:enabled="true">
    <intent-filter>
        <action android:name="android.location.PROVIDERS_CHANGED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

我正在开发基于地理围栏的应用程序.根据仅在需要时重新注册地理围栏,我们必须在之后重新注册地理围栏该应用程序已收到GEOFENCE_NOT_AVAILABLE警报.通常会在禁用NLP(Android的网络位置提供程序)后发生.

I am developing geofence based app. Based on Re-register geofences only when required we have to re register the geofences after the app has received a GEOFENCE_NOT_AVAILABLE alert. This typically happens after NLP (Android's Network Location Provider) is disabled.

启用Android的网络位置提供程序"后,我使用此广播接收器重新注册了地理围栏.

By using this broadcast receiver I re-registered the geofences when Android's Network Location Provider is enabled.

但是从API级别26开始,此广播接收器将永远无法工作.看背景执行限制.

But from API level 26 this broadcast receiver will never work. SeeBackground Execution Limits.

那么我该如何在API 26及更高版本中完成相同的任务?

So how can I achieve the same task in API 26 and higher?

注意:即使应用程序在后台,我也需要重新注册地理围栏.

Note : I need to re-register the geofences even when app is in the background.

推荐答案

您可以通过Context.registerReceiver()切换为动态注册接收者,但是恕我直言,没有可靠的方法可以使它们永久"注册,因为系统正在运行无论如何在某些条件下终止您的过程.

You could switch to registering you receivers dynamically by Context.registerReceiver(), but IMHO there is no reliable way to get them registered "forever", because the system is going to terminate your process anyway on certain conditions.

当然,您可以使用例如列入白名单的广播接收器AlarmManagerJobScheduler等.但是就电池和其他资源消耗而言,这不是最佳方法.实际上,这就是为什么Google禁用隐式广播的原因.

Sure, you could re-register them by using e.g. white listed broadcast receivers, AlarmManager, JobScheduler etc. But this is not the best way in terms of battery and other resource consumption. Actually this is the reason, why Google disabled the implicit broadcasts.

要点是:通过在奥利奥(Oreo)中禁用隐式广播,Google会强迫您使用某种重复性工作来执行此类操作.结果,您无需监听NLP状态更新,只需在重复性工作中一遍又一遍地用GeofencingClient.addGeofences(如果启用了NLP)设置地理围栏即可.

The point is: By disabling implicit broadcasts in Oreo Google forces you to use some kind of recurring job to to do things like this. As the result, you don't need to listen for the NLP state updates, you just set your geofences with GeofencingClient.addGeofences (if NLP is enabled) in your recurring job over and over again.

这篇关于LocationManager.PROVIDERS_CHANGED_ACTION将无法在API 26及更高版本上使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 17:31