【发布时间】:2019-09-03 06:18:36
【问题描述】:
我正在使用 workmanager 中的协同程序发送位置数据。 我尝试只使用workmanager,但它不做异步工作 我尝试了 ListenableWorkmanager,但这对我来说太复杂了,所以我尝试使用协程。
override fun doWork(): Result {
CoroutineScope(IO).launch {
val location = work()
fusedLocationClient!!.removeLocationUpdates(locationCallback)
string = logData(location)
}
return if(JSONObject(string).get("status") == "1"){
Result.success()
}else{
Result.retry()
}
}
我在如何从 work 函数返回位置时遇到问题
private suspend fun work():Location{
...............
fusedLocationClient!!.lastLocation.addOnSuccessListener { location ->
if (location != null) {
mCurrentLocation = location
// how do I send this location back to the fuction??
}
}.addOnFailureListener {
mLog.i(TAG, it.message)
}
return mCurrentLocation // if I do this could be a null right?
}
【问题讨论】:
标签: android location kotlin-coroutines android-workmanager