Tal*_*lha 8 android location google-play-services android-gps
在获取当前位置流时,我正在使用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();
Run Code Online (Sandbox Code Playgroud)
现在,当我调用给它提供侦听器的SettingsClient.checkLocationSettings()时,
settingsClient.checkLocationSettings(locationSettingsRequest)
.addOnCompleteListener(this)
.addOnFailureListener(this);
Run Code Online (Sandbox Code Playgroud)
它属于onFailure(),在这种情况下,github上的google官方示例采用以下方法;
检查在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;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,每当调用设置客户端的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;
}
}
Run Code Online (Sandbox Code Playgroud)
在打开GPS后,当我执行相同的操作(获取当前位置)时,SettingsClient仍会调用onFailure,尽管打开了GPS,结果始终是Activity.RESULT_CANCELED。
问题在Redmi 4X和Motorola Droid Turbo上再现
还有其他将SettingsClient与这种方法结合使用并面临任何类似问题的人吗?
归档时间: |
|
查看次数: |
314 次 |
最近记录: |