gpt4 book ai didi

Android FusedLocation 的 SettingsApi 不能多次工作

转载 作者:行者123 更新时间:2023-11-30 01:13:56 25 4
gpt4 key购买 nike

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 结果声明为类变量。仍然没有运气。我还尝试通过检查结果是否为空来仅初始化一次结果,正如预期的那样,正如谷歌文档中提到的那样,它会抛出一个异常,指出结果已被使用。

最佳答案

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

我让它工作了,在 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;
}
}
});

}

关于Android FusedLocation 的 SettingsApi 不能多次工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38151650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com