【问题标题】:Android FusedLocation's SettingsApi does not work more than onceAndroid FusedLocation 的 SettingsApi 不能多次工作
【发布时间】:2016-07-01 18:14:11
【问题描述】:

FusedLocation 有一个很棒的 AP​​I 可以检查用户是否启用了所有相关设置来获取位置。我按照文档https://developers.google.com/android/reference/com/google/android/gms/location/SettingsApi 启动了 SettingsApi。我检查用户设置的代码如下

private void checkLocationSetting()
{
    if (mLocationSettingsRequest == null)
    {
        mLocationSettingsRequest =
                new LocationSettingsRequest.Builder()
                        .addLocationRequest(mLocationRequest)
                        .setAlwaysShow(true)
                        .build();
    }

    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, mLocationSettingsRequest);

    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.
                    requestNewLocationBySettingLocationReq();
                    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(SpotToiletMap.this, REQUEST_CHECK_SETTINGS);
                    } 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.
                    MiscellaneousMethods.showGenericSnackBar(rootView, getString(R.string.location_settings_unavailable));
                    break;
                }
            }
        }
    });
}

这段代码第一次完美运行。随后,PendingResult CallBack 从未起作用。我也无法在 logcat 上找到任何日志。对我来说,这看起来像是一个实施问题。

这是重现问题的方法。

关闭 GPS。尝试调用该方法,它可以按预期完美运行。它要求我切换 n GPS。收到位置更新后,再次关闭 GPS 并调用此方法。我从来没有得到启用 GPS 的对话框。我在尝试获取用户的最新位置的按钮单击时调用此方法。

编辑: 我试图将 PendingResult 结果声明为类变量。仍然没有运气。我还尝试通过检查它是否为空来只初始化一次结果,正如预期的那样,正如谷歌文档中提到的那样,它抛出一个异常,指出结果已经被消耗。

【问题讨论】:

  • 您在应用中使用的是哪个版本的 Google Play 服务,以及您在测试时在设备上安装了哪个版本的 Google Play 服务?
  • 在我的设备上我有 Google Play 服务 9.2.56,在我的应用中我使用的是 9.0.2。
  • @DanielNugent 现在我尝试更新我的应用程序以使用最新的 9.2.0 版本。还是一样的问题
  • 另外,设备上的 Android 版本是什么?
  • 我试过 4.4.2 和 4.4.4,

标签: android google-maps gps fusedlocationproviderapi


【解决方案1】:

看起来问题在于您不是每次都重新创建LocationSettingsRequest.Builder,因为您只有在它为空时才重新创建它。

我得到了它,并在 Android 4.4.4 和 6.0.1 上进行了测试。

每次按下按钮时都会显示对话框(前提是在两次按下按钮之间设备上的位置已禁用):

public void checkLocationSetting() {
    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(30 * 1000);
    mLocationRequest.setFastestInterval(5 * 1000);

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(mLocationRequest);
    builder.setAlwaysShow(true);

    result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());

    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult result) {
            final Status status = result.getStatus();
            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(
                                MainActivity.this,
                                REQUEST_LOCATION);
                    } 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;
            }
        }
    });

}

【讨论】:

  • 我每次都尝试重新初始化Builder。我仍然无法让这个东西第二次工作。事实上,我尝试了 3 种不同的设备。所有 3 的结果相同。我还尝试重新初始化 mLocationRequest .. 也没有工作。
  • 在没有任何业务逻辑的情况下创建另一个简单活动后,我能够指出问题所在。在我的 onLocationChanged() 中,我停止了LocationUpdates。这会断开 mGoogleApiClient .. 如果 mGoogleApiClient 未连接,那么这将不起作用。这是我的要点gist.github.com/rakesh1988/03cd9f909542f7b078c921093ef9ade9
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-17
  • 2020-10-16
  • 1970-01-01
  • 1970-01-01
  • 2018-08-28
  • 1970-01-01
相关资源
最近更新 更多