为什么总是LocationSettingsStatusCodes.SUCCESS甚至GPS关闭

时间:2016-11-17 13:50:34

标签: android gps

if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

    mGoogleApiClient = new GoogleApiClient
        .Builder(this)
        .enableAutoManage(this, 34992, this)
        .addApi(LocationServices.API)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();
    mGoogleApiClient.connect();
    locationChecker(mGoogleApiClient, this);
    }

我希望在Android gps关闭时看到gps对话框。 但即使gps关闭,status.getStatusCode()总是成功,我认为这是错误的。为什么呢?

public static void locationChecker(GoogleApiClient mGoogleApiClient, final Activity activity) {

    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_NO_POWER);
    //locationRequest.setInterval(864 * 1000);
    //locationRequest.setFastestInterval(864 * 1000);
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
    .addLocationRequest(locationRequest);
    builder.setAlwaysShow(true);
    PendingResult<LocationSettingsResult> result =
    LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());

    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {

    @Override
    public void onResult(LocationSettingsResult result) {
        final Status status = result.getStatus();
        final LocationSettingsStates state = result.getLocationSettingsStates();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                // All location settings are satisfied. The client can initialize location
                // requests here.
                break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                // Location settings are not satisfied. But could be fixed by showing the user
                // a dialog.
                try {
                // Show the dialog by calling startResolutionForResult(),
                // and check the result in onActivityResult().
                status.startResolutionForResult(
                activity, 1000);
                } catch (IntentSender.SendIntentException e) {
                // Ignore the error.
                }
                break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                // Location settings are not satisfied. However, we have no way to fix the
                // settings so we won't show the dialog.
                break;
                }
        }
    });
}

2 个答案:

答案 0 :(得分:4)

更改优先级,它将检查GPS和提供商数据 将其更改为locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

答案 1 :(得分:0)

我仅在调试配置中存在此错误,发布版本中一切正常