我可以通过将检索位置的过程放在 Rx 可观察对象中来解决这个问题,特别是Single
. Single
调用对象的方法blockingGet
以同步检索位置。然后将其放置在 try catch 块中以执行重试,因为在第一次尝试期间该位置并不总是可用的。
(我知道这是一个老问题,但无论如何我都会发布一个答案,以便我可以分享我是如何做到的。很抱歉使用 Kotlin 和 RxJava!但我想每个人都会明白我的想法的要点并成为能够以他们喜欢的方式实现它。我也在使用最新的 Google Location API。)
// Use an executor to prevent blocking the thread where
// this method will be called. Also, DO NOT call this on
// the main thread!!!
fun tryRetrieveLocationSync(flc: FusedLocationProviderClient,
executor: Executor, numOfRetries: Int = 3,
retryWaitTime: Long = 500): Location {
var location: Location
var i = 1
while (true) {
try {
// Place the method call inside a try catch block
// because `Single#blockingGet` will throw any exception
// that was provided to the `Emitter#onError` as an argument.
location = retrieveLocationSync(flc, executor)
} catch (e: NoLocationDataException) {
if (i <= numOfRetries) {
// The value from the `FusedLocationProviderClient#lastLocation`
// task usually becomes available after less than second (tried
// and tested!!), but it's really up to you.
SystemClock.sleep(retryWaitTime * i)
i++
} else {
throw e // Give up once all the retries have been used.
}
} catch (e: Exception) {
// Rethrow anything else that was thrown from
// the `Single#blockingGet`.
throw e
}
}
return location
}
private fun retrieveLocationSync(flc: FusedLocationProviderClient,
executor: Executor): Location {
return Single.create<Location> { emitter ->
val task = flc.lastLocation
task.addOnCompleteListener(executor, OnCompleteListener { // it ->
if (it.isSuccessful) {
if (it.result != null) {
emitter.onSuccess(it.result)
} else {
// There is no location data available probably because
// the location services has just been enabled, the device
// has just been turned on, or no other applications has
// requested the device's location.
emitter.onError(NoLocationDataException())
}
} else {
// I haven't encountered any exception here but this is
// just to make sure everything's catchable.
emitter.onError(it.exception ?: RuntimeException())
}
})
}.blockingGet()
}