gpt4 book ai didi

android - SettingsClient 的位置请求总是得到 RESULT_CANCELED

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:17:32 25 4
gpt4 key购买 nike

在获取当前位置流时,我使用 SettingsClient 根据当前 LocationRequest 检查是否满足位置设置。目前,我的优先级设置为 HIGH_ACCURACY,这需要不惜一切代价启用 GPS。

        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
settingsClient = LocationServices.getSettingsClient(this);
locationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(500)
.setFastestInterval(500);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(locationRequest);
locationSettingsRequest = builder.build();

现在,当我调用 SettingsClient.checkLocationSettings() 给它监听器时,

settingsClient.checkLocationSettings(locationSettingsRequest)
.addOnCompleteListener(this)
.addOnFailureListener(this);

落入onFailure(),google official samples on github在这种情况下采用以下方法;

检查在 onFailure() 中收到的异常状态代码,如果它是 LocationSettingsStatusCodes.RESOLUTION_REQUIRED,然后调用 startResolutionForResult(),它允许我们启用 GPS,这将使用 onActivityResult 等待结果。

    @Override
public void onFailure(@NonNull Exception e) {

int statusCode = ((ApiException) e).getStatusCode();
switch (statusCode) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:

try {
// Show the dialog by calling startResolutionForResult(), and check the
// result in onActivityResult().
ResolvableApiException rae = (ResolvableApiException) e;
rae.startResolutionForResult(LocationActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sie) {
showLocationError();
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
showError();
break;
}
}

问题是,每当调用设置客户端的onFailure和调用startResolution时,它总是属于Activity.RESULT_CANCELED的情况。但奇怪的是,虽然这说取消了,但GPS会在几秒钟内打开。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
// Check for the integer request code originally supplied to startResolutionForResult().
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK:
// fetch location here
break;
case Activity.RESULT_CANCELED:
// this is called immediately first time
// and second time even when GPS is on
break;
}
break;
}
}

在 GPS 打开后,当我执行相同的操作(获取当前位置)时,SettingsClient 仍然调用 onFailure,尽管 GPS 打开,结果始终是 Activity.RESULT_CANCELED。

问题在 Redmi 4X 和 Motorola Droid Turbo 上重现

还有其他人使用过这种方法的 SettingsClient 并遇到过任何类似的问题吗?

Here is the official sample I am following

This bug is also reported on official GitHub issues page

最佳答案

我发现在将 PRIORITY_HIGH_ACCURACY 添加到请求时发生这种情况是因为定位模式设置为省电,所以即使回答肯定地打开定位服务(确实打开),模式仍然是Battery Saving 无法处理 PRIORITY_HIGH_ACCURACY 请求,因此您会收到 RESULT_CANCELED 指示您的请求未得到满足。我不知道有什么方法可以请求打开两个定位服务并将模式更改为高精度,所以我只是取消了优先级,因为它对我的用例来说并不那么重要。

关于android - SettingsClient 的位置请求总是得到 RESULT_CANCELED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55550481/

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