【发布时间】:2016-07-01 18:14:11
【问题描述】:
FusedLocation 有一个很棒的 API 可以检查用户是否启用了所有相关设置来获取位置。我按照文档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