适用于Android的Google Play服务.如果禁用wifi,位置客户端不会更新位置

asi*_*ura 19 android location google-play-services

我们的应用程序请求使用LocationClient和IntentService更新位置.如果用户在手机设置中禁用wifi,则位置根本不会更新.

我们尝试使用PRIORITY_HIGH_ACCURACY测试应用,并在禁用wifi时更新位置信息.

如果用户在没有wifi网络可用的地方移动,则会出现同样的问题.

如果手机无法通过wifi更新位置,我认为正确的行为是使用其他手机传感器(如GPS)进行更新.

重现步骤:

  1. 使用待定意图启动更新位置.带参数

    LocationRequest request = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
            .setInterval(5 * 60 * 1000)
            .setFastestInterval(60*1000)
            .setSmallestDisplacement(70);
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在手机设置中禁用wifi或在没有无线网络的国家/地区使用手机.

在Android 4.4,4.3,4.1,4.0,2.3.3上测试过.(Nexus7,Nexus4,三星GS2,HTC Wildfire等)

public class TrackerService extends IntentService {
    private void startUpdateLocation() {
        PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),
            0, new Intent(ACTION_LOCATION_UPDATED), 0);
        LocationRequest request = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
            .setInterval(5 * 60 * 1000)
            .setFastestInterval(60*1000)
            .setSmallestDisplacement(70);

        getLocationClient().requestLocationUpdates(request, pendingIntent);
        Log.d(Utils.getMethodName(), "Location update started");
    }

    private LocationClient getLocationClient() {
        if (mLocationClient != null && mLocationClient.isConnected()) return mLocationClient;
        mLocationClient = new LocationClient(this,
            new GooglePlayServicesClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle bundle) {
                    Log.d(Utils.getMethodName(), "Location client. Connected");
                }

                @Override
                public void onDisconnected() {
                    Log.d(Utils.getMethodName(), "Location client. Disconnected");
                }
            },
            new GooglePlayServicesClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult connectionResult) {
                    throw new IllegalStateException("Failed connection to location manager " + connectionResult.toString());
                }
            }
        );
        mLocationClient.connect();
        try {
            while (mLocationClient.isConnecting()) {
                Thread.sleep(200);
            }
        } catch (InterruptedException e) {
            throw new IllegalStateException("Thread interrupted", e);
        }
        return mLocationClient;
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试发送错误报告https://code.google.com/p/android/issues/detail?id=63110

Android的开发者无法帮助.

我在哪里可以报告Google Play服务中的错误?

这是Google Play服务中的错误吗?

你可以提供什么工作?

我们不想使用PRIORITY_HIGH_ACCURACY,因为它会耗尽手机电池.我们的应用程序跟踪手机位置,它不应取决于WiFi设置和WiFi网络可用性.

asi*_*ura 12

我找到了解决方案

Google更改了位置客户端的文档.现在它说:

public static final int PRIORITY_BALANCED_POWER_ACCURACY

与setPriority(int)一起使用以请求"块"级别的准确性.

块级精度被认为是大约100米的精度.使用诸如此类的粗略精度通常会消耗更少的功率.

使用粗略的准确度 - 我认为这意味着如果优先级为PRIORITY_BALANCED_POWER_ACCURACY,则位置客户端不会将GPS用于更新位置.

因此,对于使用GPS,您应该使用PRIORITY_HIGH_ACCURACY.

  • 我能够重现这种行为,好的捕获,也是WTF,谷歌. (4认同)
  • 这是行不通的,即使在'PRIORITY_BALANCED_POWER_ACCURACY`之后,`onLocationChanged()`也没有调用.即使在1小时后也没有来自`requestLocationUpdates`的回调 (2认同)

And*_*eas 6

让我试着给你一个答案.直到几分钟前我遇到了同样的问题,并希望在这里有答案.

我将描述我的问题和解决方案:

问题:在使用Google Play服务LocationClient时,如果没有互联网连接,我没有获得任何位置更新.启用GPS.

我的代码很稳定,可以使用Internet,但现场测试失败了.

可能的根本原因:1)LocationClient被吸2)位置客户端没有获得任何gps更新

解决方案:为了测试这个,我尝试使用谷歌地图应用程序.它抱怨没有互联网连接,所以我给了它一个.然后我试图找到自己,但出现了一个新的屏幕,说我必须允许"谷歌应用程序"访问我的位置.它说,这不会影响第三方应用程序,而不是谷歌,结果证明是完全废话.当我启用"只有谷歌应用程序"访问我的位置时,我自己的应用程序突然在几秒钟内开始行动.Android的开发已经充满了这些"谷歌时刻".