- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用此函数来获取用户位置,然后将该位置作为参数返回给另一个函数。但是,编译器不会等待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/
我读过这篇文章answer其中表示使用 get() 函数时无需删除任何监听器,这是否适用于 set()、update()和delete()? 如何区分监听器何时需要移除?我很困惑,几天来我一直在与听众
我有一个 MyActivity由 FragmentA 组成和 FragmentB和 MyActivityPresenter . fragment A: @OnClick(R.id.proceed_si
AuthResult 为红色。请帮我。也无法导入。它说找不到符号。他们有什么问题吗?我尝试了很多,但没有找到解决办法。 单击 ALT+ENTER 时,不显示导入任何内容 LoginActivity.j
我正在使用实时数据库,感觉以下两种方法有点混淆: ROOT_REF.updateChildren(map, new DatabaseReference.CompletionListener() {
我正在尝试实现 Firebase 远程配置: override fun onCreate(savedInstanceState: Bundle?) { val configSettings =
我正在尝试获取智能手机的 GPS,如下面的代码: 在 Activity.java ,我使用以下代码获取 GPS。 private GPSClass gps; gps.getGPSLocation();
我正在尝试读取我的云 Firestore 中的数据并将其放入名为“urlList”的数组中。 arrayList 在 onComplete 中,如果我在那里记录数组,它就可以工作(它充满了它应该拥有的
我想在 onCompleteListener 成功时获得一些返回值,但对于 Kotlin 和 lambda,我无法获得。我读了这个doc . 我正在使用这个 onCompleteListener 从
CreateView 方法上的自定义对话框在 Firebase Firestore onCompletelistener 获取要列出的数据之前返回。这就是为什么没有 View 显示。 我用CardVi
我正在使用 Cloud Firestore 数据库填充 Android 应用中的 RecyclerView。我通过在 fragment 的 onAttach 方法中使用任务来获取数据。我需要能够使用
我在从 Firestore 获取数据时遇到问题,在 Java 代码中我们可以这样做: DocumentReference docRef = db.collection("cities").docume
我正在构建一个使用 Firebase 做两件事的客户端应用程序: 用户认证 使用实时数据库 我已经成功地在我的客户端和后端服务器上正确设置了所有内容(使用 Firebase 的 Admin SDK),
我在 Java 8 SE 中使用 OnCompleteListerner 时遇到错误匿名 new OnCompleteListener() 可以用 lambda less 替换... (Ctrl+F1
这个问题在这里已经有了答案: How to return a DocumentSnapShot as a result of a method? (2 个答案) 关闭 3 年前。 我正在使用 fir
我编写了一段代码来使用 Firebase Auth 创建新帐户: public static int signUp(String email, String password) { mAuth.c
下面是代码。我在编码方面没有太多经验。希望你能帮助我!非常感谢=) 我的错误是: error: incompatible types: > cannot be converted to OnCompl
我是一名优秀的程序员,十分优秀!