首页 文章

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

提问于
浏览
18

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

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

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

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

重现步骤:

  • 使用待定意图开始更新位置 . 带参数
LocationRequest request = LocationRequest.create()
        .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
        .setInterval(5 * 60 * 1000)
        .setFastestInterval(60*1000)
        .setSmallestDisplacement(70);
  • 在手机设置中禁用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;
    }
}

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

Android的开发者无法帮助 .

Where can I report about bugs in Google Play Services?

Is it a bug in Google Play Services?

What work around could you offer?

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

3 回答

  • 10

    我找到了解决方案

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

    public static final int PRIORITY_BALANCED_POWER_ACCURACY与setPriority(int)一起使用以请求“块”级别的准确性 . 块级精度被认为是大约100米的精度 . 使用诸如此类的粗略精度通常会消耗更少的功率 .

    Using a coarse accuracy - 我认为这意味着如果优先级为PRIORITY_BALANCED_POWER_ACCURACY,则位置客户端不会将GPS用于更新位置 .

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

  • 6

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

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

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

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

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

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

  • 1

    如果有人还在想,

    Google documentation says:

    如果您同时使用NETWORK_PROVIDER和GPS_PROVIDER,则只需要请求ACCESS_FINE_LOCATION权限,因为它包含两个提供程序的权限 . ACCESS_COARSE_LOCATION的权限仅允许访问NETWORK_PROVIDER .

相关问题