每5秒获取android位置的最佳方法是什么

时间:2016-10-17 14:30:21

标签: android gps locationlistener

使用LocationListener每5秒在Android中获取以下内容的最佳方法和最佳省电模式是什么:

  1. 位置(纬度,经度)
  2. 精度
  3. 高度
  4. 速度
  5. 轴承
  6. ...
  7. 我试过这个(LocationListener):

    $

    但是如果位置发生了变化,这只会更新,但我需要每5秒才有一次。也许有人可以给我一个例子。

    感谢您的回答。

1 个答案:

答案 0 :(得分:0)

使用rxJava的方法Observable.interval(5,TimeUnit.Seconds)。 ... 并且在不再需要更新时从中进行ubsubscribe。 在事件处理(subscribe())方法手动轮询位置。

Subscription pollingConnection = Observable.interval(5, TimeUnit.SECONDS)
                .observeOn(Schedulers.computation())
                .map(t -> {
                    // get all your readings manually (it is executed in background because of
                    //  .observeOn(Schedulers.computation())
                    return  t;
                })
                .subscribe(t -> {
                }, e -> {
                });

        // call pollingConnection.unsubscribe(); when update is no longer needed