使用LocationSettingsRequest请求高精度位置不更新位置设置

时间:2019-04-04 15:55:19

标签: android google-play-services android-location

我正在使用新的LocationSettingsRequest系统来请求高精度位置。例如,如果我的设备设置为省电模式,则会提示系统对话框以更新位置精度。在大多数情况下,这是可行的,但在某些设备中,会发生以下情况:

  1. 如果将设备设置为省电模式,则系统对话框会询问gps许可,并且仅在确认后才更改为gps。
  2. 如果设备仅设置为gps,则系统会要求wifi和Google服务来改善位置,并在确认后更改为省电模式位置。

这是我设置LocationSettingsRequest的方式:

val builder = LocationSettingsRequest.Builder()
    .addLocationRequest(createLocationRequest(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY))
    .addLocationRequest(createLocationRequest(LocationRequest.PRIORITY_HIGH_ACCURACY))
    .setAlwaysShow(true)

settingsClient.checkLocationSettings(builder.build())

这会触发Google Play服务系统对话框以更新位置准确性设置。但是例如,在运行Android 7.0的Samsung Galaxy S6上,该位置永远不会更新为高精度。

当我尝试使用here的Google位置示例时,也会发生同样的情况。在locationupdates文件夹中,检查称为设置分辨率的startLocationUpdates()方法:

private void startLocationUpdates() {
    // Begin by checking if the device has the necessary location settings.
    mSettingsClient.checkLocationSettings(mLocationSettingsRequest)
            .addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
                @Override
                public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
                    Log.i(TAG, "All location settings are satisfied.");

                    //noinspection MissingPermission
                    mFusedLocationClient.requestLocationUpdates(mLocationRequest,
                            mLocationCallback, Looper.myLooper());

                    updateUI();
                }
            })
            .addOnFailureListener(this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    int statusCode = ((ApiException) e).getStatusCode();
                    switch (statusCode) {
                        case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                            Log.i(TAG, "Location settings are not satisfied. Attempting to upgrade " +
                                    "location settings ");
                            try {
                                // Show the dialog by calling startResolutionForResult(), and check the
                                // result in onActivityResult().
                                ResolvableApiException rae = (ResolvableApiException) e;
                                rae.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                            } catch (IntentSender.SendIntentException sie) {
                                Log.i(TAG, "PendingIntent unable to execute request.");
                            }
                            break;
                        case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                            String errorMessage = "Location settings are inadequate, and cannot be " +
                                    "fixed here. Fix in Settings.";
                            Log.e(TAG, errorMessage);
                            Toast.makeText(MainActivity.this, errorMessage, Toast.LENGTH_LONG).show();
                            mRequestingLocationUpdates = false;
                    }

                    updateUI();
                }
            });
}

在此Samsung设备上运行此示例应用程序会导致相同的问题。 onActivityResult()被称为Activity.RESULT_CANCELED

有人经历过类似的事情吗?关于如何解决该问题有什么想法吗?

1 个答案:

答案 0 :(得分:1)

在android 8.0和8.1设备中也有同样的问题。我通过将位置模式更改为“省电”解决了该问题。

mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

替换了HIGH_ACCURACY请求。