我正在使用此函数来获取用户位置,然后将该位置作为参数返回给另一个函数。但是,编译器不会等待OnComleteListener完成才返回null-因为位置变量已初始化为null。
谁能帮助我让编译器等待侦听器的定位结果?我可以从设备中获取位置信息,因为我可以在文本 View 中进行打印。
这是我指的功能:
@SuppressLint("MissingPermission", "SetTextI18n")
fun getLastLocation(): Location? {
var holder: Location? = null
Log.e(TAG,"CALLED")
if(CheckPermission()) {
if(isLocationEnabled()) {
fusedLocationProviderClient.lastLocation.addOnCompleteListener {task ->
var location = task.result
Log.e(TAG,"Location: $location")
if(location == null) {
getNewLocation()
} else {
holder = location
Log.e(TAG, "Holder1 = $holder")
locationText.text = "Your current coordinates are :\nLat: " + location.latitude + " ; Long: " + location.longitude
}
}
Log.e(TAG, "Holder2 = $holder")
} else {
Toast.makeText(this, "Please enable your location service", Toast.LENGTH_LONG).show()
}
} else {
RequestPermission()
}
Log.e(TAG, "Holder3 = $holder")
return holder
}
最佳答案
如前所述,您可以使用 suspendCoroutine
异步地 (悬浮式)等待完成。
suspend fun <T> Task<T>.await(): T {
if (isComplete) {
val e = exception
return if (e == null) {
if (isCanceled) {
throw CancellationException(
"Task $this was cancelled normally.")
} else {
result
}
} else {
throw e
}
}
return suspendCancellableCoroutine { cont ->
addOnCompleteListener {
val e = exception
if (e == null) {
if (isCanceled) cont.cancel() else cont.resume(result)
} else {
cont.resumeWithException(e)
}
}
}
}
// Usage:
try{
val location = fusedLocationProviderClient.lastLocation.await()
} catch(e:Exception){
//handle exception
}
// do something with location, that should be executed after location has fetched
如果不运行代码以在协程内部等待,则可以将runBlocking的代码包装到同步的等待中(通过阻止当前线程)。val location = runBlocking { fusedLocationProviderClient.lastLocation.await() }
// do something with location, that should be executed after location has fetched
关于android - 如何让编译器等待OnCompleteListener Kotlin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63802432/