地理围栏不可用以及如何处理 [英] Geofences not available and how to handle it

查看:179
本文介绍了地理围栏不可用以及如何处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用android的地理围栏,在大多数手机上都可以正常工作, 但其中一些只是不起作用(我的错误日志中显示"geofences not available").

I'm working with geofences on android, it's all working fine on most phones, but on some of them, it's just not working (showing "geofences not available" in my error logs).

某些用户没有为Google Play服务启用位置跟踪. 我认为这就是地理围栏无法在其手机上运行的原因. (对吗?)

Some users don't have their location tracking enabled for Google Play Services. I think that is the reason why geofencing is not working on their phones. (right?)

现在,我想要一种检测此情况的方法. (通知用户,并最终向他们展示如何解决该问题) 我知道有一种方法可以知道Google Play服务是否可用(并且我已经这样做了),但这并没有说明是否启用了位置服务.

Now I want a way to detect this. (inform the user and eventually show them how to fix it) I know there is a way to know wether google play services is available or not (and I already do this), but that doesn't say anything about the location services being enabled or not.

我知道我可以在添加或删除地理围栏时进行检查,但有一个错误代码(地理围栏不可用

I know that I can check it while adding or removing geofence, there is an error code for it (geofence not available Location Status Codes), but that's not enough. Some users disable the location services after they added the geofences. Then I have no way of knowing this, and my app won't work. (complaining users etc.). At least I have to inform the user when they open the app to check what's wrong.

有人对如何执行此操作有想法吗? 我现在能做的最好的事情就是在应用启动时添加和删除虚拟地理围栏,但是必须有更好的方法吗?

Does someone have an idea on how to do this? The best I can do now is adding and removing a dummy geofence on app boot, but there has to be a better way?

我使用有问题的设备对其进行了测试,删除该应用程序并重新安装似乎可以解决该问题. 我在第一次启动时没有做任何特别的事情,所以这真的很奇怪. 看来Google Play服务连接存在问题,并且重新安装该应用对这些服务有特殊的作用. 这可能吗?连接它们时,它没有任何错误,但是当我尝试设置地理围栏时(见上文),它确实存在.

I tested it with a device that had the issue, removing the app and reinstalling seems to fix the problem. I'm not doing anything special on the first boot, so this is really weird. It looks as if there is a problem with the google play services connection, and reinstalling the app does something special with these services. Is this possible? It gives no errors while connecting to them, but it did when I tried to set geofences (see above).

推荐答案

如果GPS提供商或网络提供商出于任何原因被禁用,则地理围栏将被禁用(并且未注册).这将导致您的onAddGeofencesResult()以错误状态代码(1000)被调用.您可以在那里检测到它.

The geofences will get disabled (and unregistered) if the GPS Provider or the Network Provider are disabled for whatever reason. That will cause your onAddGeofencesResult() to get called with an status code of error (1000). You can detect it right there.

或者,您可以通过实现一个响应android.location.PROVIDERS_CHANGED而被调用的Receiver来检测到这一点.请记住将接收器添加到AndroidManifest

Or, you detect this by implementing a Receiver that gets called in response to android.location.PROVIDERS_CHANGED. Remember to add the receiver to the AndroidManifest

<receiver android:name=".service.GeofenceProvidersChangedBroadcastReceiver">
    <intent-filter>
        <action android:name="android.location.PROVIDERS_CHANGED"/>
    </intent-filter>
</receiver>

在接收器实现的onReceive()上,获取位置管理器并测试提供商的状态:

On the receiver implementation, on its onReceive(), get the location manager and test for the providers status:

public class ProvidersChangedBroadcastReceiver extends WakefulBroadcastReceiver {

    :
    :

    @Override
    public void onReceive(Context context, Intent intent) {

        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            // GPS_PROVIDER IS enabled...
        } else {
            // GPS_PROVIDER is NOT enabled...
        }

        if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            // NETWORK_PROVIDER IS enabled...
        } else {
            // NETWORK_PROVIDER is NOT enabled...
        }

        :
        :
        :

        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) &&
        locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        // We are good for geofencing as both GPS and Network providers are enabled....
    }

    :
    :
    :

}

这篇关于地理围栏不可用以及如何处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆