gpt4 book ai didi

android - 如何在更改时检查位置设置是否失败

转载 作者:太空狗 更新时间:2023-10-29 13:50:13 33 4
gpt4 key购买 nike

我正在创建 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 是否是状态代码,并且我可以提示用户更改它。任何人都可以告诉我我可以在代码或手机设置中做什么来测试这种情况。

最佳答案

维尼特·辛格,

我不知道我的回答是否为时已晚,但当我的设备位置被禁用时我遇到了这个异常。

我收到了这个异常(exception):

com.google.android.gms.common.api.ResolvableApiException: 6: RESOLUTION_REQUIRED

希望对您有所帮助。

关于android - 如何在更改时检查位置设置是否失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48965834/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com