6

由于我们的应用程序的工作方式,我需要同步获取用户的当前位置。我们当前的实现使用com.google.android.gms.location.LocationListener来接收位置更新。问题是,我需要在尝试第一次调用之前更新位置服务器端,否则用户会得到错误的数据。

LocationServices.FusedLocationApi.getLastLocation()不适合这个,因为a)它似乎总是返回null(无论出于何种原因)和b)我们已经在服务器端保存了用户最后一个已知位置,因此检索最后一个已知位置并将其发送到服务器是多余的.

伪代码

//this is the first call I need to make
webservice.appStart(AppStartBody body);
//then I want to retrieve the current location and send it to the server
webservice.setGeoLocation(getCurrentLocation());
//finally, I retrieve the items on the server based on the location
webservice.getListItems();

等待第一个位置更新事件是我想避免的一种可能性,因为我不知道它会在多长时间内触发,并且我可能不得不让我的用户在加载屏幕中停留很长时间,进而失去他们,因为没有人喜欢等待。

4

1 回答 1

2

我可以通过将检索位置的过程放在 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()
}
于 2018-03-09T14:10:33.243 回答