【发布时间】:2018-08-04 13:42:41
【问题描述】:
我正在创建 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 是否是状态代码,并且我可以提示用户更改它。谁能告诉我我可以在代码或手机设置中做什么来测试这种情况。
【问题讨论】: