Vin*_*ngh 5 android android-location
我正在创建 LocationRequest 类的 locationrequest 对象,其方法用于确定我的应用程序所需的位置准确度级别。
private LocationRequest mLocationRequest;
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(2000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
Run Code Online (Sandbox Code Playgroud)
然后创建一个 LocationSettingsRequest.Builder 对象,然后向其中添加位置请求对象。
new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest);
Run Code Online (Sandbox Code Playgroud)
根据 Android 文档,SettingsClient 负责确保设备的系统设置针对应用程序的位置需求进行了正确配置。
SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task =
client.checkLocationSettings(builder.build());
Run Code Online (Sandbox Code Playgroud)
文档指出,当任务完成时,客户端可以通过查看 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;
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
根据我的理解,上面的代码检查完成的任务是否能够接受我们执行时更改的位置设置
该文档还指出,如果状态代码是 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;
}
}
Run Code Online (Sandbox Code Playgroud)
我希望发生这种情况,我可以测试 RESOLUTION_REQUIRED 是否是状态代码,并且我可以提示用户更改它。任何人都可以告诉我我可以在代码或手机设置中做什么来测试这种情况。
维尼特·辛格,
我不知道我的回答是否为时已晚,但当我的设备位置被禁用时,我遇到了此异常。
我收到这个例外:
com.google.android.gms.common.api.ResolvableApiException: 6: RESOLUTION_REQUIRED
Run Code Online (Sandbox Code Playgroud)
希望它能有所帮助。
| 归档时间: |
|
| 查看次数: |
2496 次 |
| 最近记录: |