【发布时间】:2019-04-22 12:03:13
【问题描述】:
val request = LocationRequest()
request.interval = 1000 * 60
request.fastestInterval = 1000 * 30
request.smallestDisplacement = 10f
request.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
val builder = LocationSettingsRequest.Builder().addLocationRequest(request)
builder.setAlwaysShow(true)
val result = LocationServices.getSettingsClient(this).checkLocationSettings(builder.build())
result.addOnFailureListener {
if (it is 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().
it.startResolutionForResult(this, 1)
} catch (sendEx: IntentSender.SendIntentException) {
// Ignore the error.
}
}
}
以上代码用于要求用户打开位置。但是最近我发现有时即使打开了位置,它也会要求打开位置。
编辑:1 我最近发现,如果设备开启了省电模式或设备位置精度设置为 LOW,那么此请求将失败并显示相同的状态代码。
【问题讨论】: