java - addOnFailureListener() 如果用户曾经启用并在一段时间后禁用位置,则不会调用。使用 LocationServices.getSettingsClient() 时
问题描述
- 我曾经
LocationServices.getSettingsClient()
删除 depriated
LocationServices.SettingsApi.checkLocationSettings()
。 - 如果位置被禁用,它会在 mainActivity 启动时要求用户第一次启用位置。
- 如果在两者之间我禁用位置。它不是要求启用位置。
result.addOnSuccessListener
即使我的位置被禁用,也会调用手段。 - 如果位置在中间被禁用,它应该询问
result.addOnFailureListener
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getContext());
mSettingsClient = LocationServices.getSettingsClient(getContext());
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(mLocationRequest);
mLocationSettingsRequest = builder.build();
mLocationService = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
Task<LocationSettingsResponse> result =
LocationServices.getSettingsClient(getContext()).checkLocationSettings(builder.build());
result.addOnSuccessListener(getActivity(), new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
LocationSettingsResponse response =
task.getResult(ApiException.class);
LocationSettingsStates locationSettingsStates = response.getLocationSettingsStates();
if (!locationSettingsStates.isGpsPresent() || !locationSettingsStates.isGpsUsable()) {
//check point 4
}
});
result.addOnFailureListener(getActivity(), new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ResolvableApiException) {
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(getActivity(),
REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error.
}
}
}
});
- 在这里,我需要使用以下代码打开位置启用对话框:
final Status status = result.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
Log.i(TAG, "All location settings are satisfied.");
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to upgrade location settings ");
try {
// Show the dialog by calling startResolutionForResult(), and check the result
// in onActivityResult().
status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
Log.i(TAG, "PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog not created.");
break;
}
5 .但是使用give 不会给出我在 catch 块中收到的SettingsClient
onSucess
statusCode
Task<LocationSettingsResponse> result
更新
第一个标签是尝试OnComplete()
位置默认情况下的一部分,第二次我关闭位置并转到其他Fragment
并再次来到主要片段。即使我的位置被禁用,Tt 也执行了尝试部分
解决方案
推荐阅读
- gitlab - 如何让 gitlab 管道停止并要求我输入变量?
- flutter - 如何在 Flutter/Dart 中导入特定于平台的依赖项?(结合 Web 与 Android/iOS)
- laravel - Web 开发堆栈 - GONE WILD --- 最佳实践架构和部署?
- reactjs - 为什么延迟加载不是 React 的默认设置?
- python - 如何使用 python 从 JSON 文件中删除空格(仅来自 JSON 密钥)
- c - 数组的统计
- vb.net - 检索对象名称
- angular - 使用 nativescript / angular 填充现有 PDF 文件中的表单字段
- scala - 如何获取加入 Spark 产生的行数
- azure-functions - Azure Function 未注册身份验证处理程序