Android FusedLocation 的 SettingsApi 不能工作超过一次
Android FusedLocation's SettingsApi does not work more than once
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 结果声明为 class 变量。仍然没有运气。我还尝试通过检查结果是否为空来仅初始化一次结果,正如预期的那样,并且如 Google 的文档中所述,它抛出一个异常,指出结果已被消耗。
看起来问题在于您并不是每次都重新创建 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;
}
}
});
}
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 结果声明为 class 变量。仍然没有运气。我还尝试通过检查结果是否为空来仅初始化一次结果,正如预期的那样,并且如 Google 的文档中所述,它抛出一个异常,指出结果已被消耗。
看起来问题在于您并不是每次都重新创建 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;
}
}
});
}