我正在创建LocationRequest类的locationrequest对象,其方法用于确定我的应用所需的位置精确度。
private LocationRequest mLocationRequest;
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(2000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
然后创建一个LocationSettingsRequest.Builder对象,然后将位置请求对象添加到它。
new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest);
根据Android文档,SettingsClient负责确保为应用程序的位置需求正确配置设备的系统设置。
SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task =
client.checkLocationSettings(builder.build());
文档指出,当任务完成时,客户端可以通过查看LocationSettingsResponse对象中的状态代码来检查位置设置。
task.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>()
{
@Override
public void onComplete(Task<LocationSettingsResponse> task) {
try {
LocationSettingsResponse response = task.getResult(ApiException.class);
// All location settings are satisfied. The client can initialize location
// requests here.
} catch (ApiException exception) {
Log.v(" Failed ", String.valueOf(exception.getStatusCode()));
switch (exception.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the
// user a dialog.
// Cast to a resolvable exception.
ResolvableApiException resolvable = (ResolvableApiException) exception;
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
try {
resolvable.startResolutionForResult(
MapsActivity.this,
REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
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;
}
}
}
});
根据我的理解,上面的代码检查完成的任务是否能够接受我们做的时更改的位置设置
该文档还指出,如果状态代码为RESOLUTION_REQUIRED,则客户端可以调用startResolutionForResult(Activity,int)来调出对话框,要求用户修改位置设置以满足这些请求。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
switch (requestCode) {
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK:
// All required changes were successfully made
Toast.makeText(getBaseContext(), "All good", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
// The user was asked to change settings, but chose not to
break;
default:
break;
}
break;
}
}
我希望发生这种情况,我可以测试RESOLUTION_REQUIRED是状态代码,我可以提示用户更改它。任何人都可以告诉我在代码或手机设置中我可以做些什么来测试这种情况。
答案 0 :(得分:1)
Vinit Singh,
我不知道我的回答是否为时已晚,但是当我禁用设备位置时出现此异常。
我收到这个称呼:
com.google.android.gms.common.api.ResolvableApiException: 6: RESOLUTION_REQUIRED
希望它会有所帮助。
答案 1 :(得分:0)
问题是,与documentation相比,我们无法使用传递给{{1}的status
的{{1}}字段 },因为是私人。
任务完成后,您的应用可以通过查看
LocationSettingsResponse
对象中的状态代码来检查位置设置。
我得出的结论是,OnSuccessListener
仅在设置满足要求时被调用,否则,将调用 传递给LocationSettingsResponse
(当它为OnSuccessListener
时可以用来解决问题)。