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结果声明为类变量。仍然没有运气。我还尝试通过检查它是否为null来初始化结果,正如预期的那样,并且正如Google的文档中所提到的,它会引发一个异常,提到结果已经被消耗。
答案 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 4.4.4和6.0.1上进行了测试。
每次按下按钮时都会显示该对话框(前提是按钮按下时设备上禁用了位置):
#import <Cocoa/Cocoa.h>