【问题标题】:What is the best way to get the location in android every 5 sec每5秒在android中获取位置的最佳方法是什么
【发布时间】:2016-10-17 14:30:21
【问题描述】:

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

  1. 位置(纬度、经度)
  2. 准确度
  3. 海拔高度
  4. 速度
  5. 轴承
  6. ...

我试过这个(LocationListener):

 @Override
public void onLocationChanged(Location location) {
   // ...Code...
}

但这只有在位置发生变化时才会更新,但我需要每 5 秒一次,而且只有在那里。也许有人可以给我举个例子。

感谢您的回答。

【问题讨论】:

  • 如果位置没有变化,就和之前一样使用...
  • 但是如果位置发生变化,这 5 秒前会有更新。
  • 您阅读过requestLocationUpdate 的文档吗?你没看到minTime 参数吗? "minTime long: 位置更新的最小时间间隔,以毫秒为单位"
  • 是的,但它不起作用。 Alex Shutov 的分析器有效。

标签: android gps locationlistener


【解决方案1】:

使用 rxJava 的方法 Observable.interval(5, TimeUnit.Seconds)。 ... 并在不再需要更新时订阅它。 在事件处理 (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

【讨论】:

  • 你能给我一个样品吗??
猜你喜欢
  • 2018-03-17
  • 1970-01-01
  • 1970-01-01
  • 2011-07-05
  • 2016-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多