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

标签 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 的三星 Galaxy S6 上,该位置永远不会更新到高精度。

当我尝试可用的 Google 位置示例时会发生同样的情况 here .在 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();
                }
            });
}

在此三星设备上运行此示例应用程序会导致同样的问题。 onActivityResult()Activity.RESULT_CANCELED 情况下被调用。

有人遇到过类似的事情吗?关于如何解决该问题的任何想法?

最佳答案

在 android 8.0 和 8.1 设备上有同样的问题。我通过将定位模式更改为省电解决了这个问题。

mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

替换了 HIGH_ACCURACY 请求。

关于android - 使用 LocationSettingsRequest 请求高精度位置不更新位置设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55520315/

相关文章:

android - 在 Eclipse 中调试 Android 设备时 PC 崩溃

android - 对话框中的背景颜色显示不正确

带有 ADT 的 Android Studio 与 Eclipse (2015)

android - NearbyBackgroundBeacons 演示在订阅时返回未知状态代码 2801

android - 构建 android chrome cast 视频示例时出错

android - 谷歌播放服务 : how to check if there is currently "active" pending intent callback registered to location updates/activity recognition?

android - 为什么 locationManager.getBestProvider() 返回 null? (连接 5x)

android - 构建分布式 Android 应用程序

android - 如何将 XP 点数添加到 Google Play 游戏?

java - 如何?编写 Android 应用程序代码以将用户的位置设置更改为关闭?