java - requestLocationUpdates 与 getLastLocation

标签 java android location locationmanager

从 LocationManager 调用 requestLocationUpdates 是否会更新 getLastLocation 返回的位置?

即,我可以这样做吗:

  1. 调用 getLastLocation。它返回 null,然后:
  2. 调用requestLocationUpdates;
  3. 在我的监听器上接收 onLocationChanged,但不执行任何操作

然后,调用 getLastLocation 并获取一些内容!=null?

谢谢!

最佳答案

如果 LocationManager 有先前的位置读数,getLastLocation 将返回最后的位置。如果 LocationManager 没有先前的位置,则将返回 null。

requestLocationUpdates 将请求持续的位置更新,直到您取消订阅位置更新为止。

更多详细信息可以在这里找到:

https://developer.android.com/training/location/retrieve-current.html

以下解释了为什么 getLastLocation 可能返回 null:

Location is turned off in the device settings. The result could be null even if the last location was previously retrieved because disabling location also clears the cache.

The device never recorded its location, which could be the case of a new device or a device that has been restored to factory settings.

Google Play services on the device has restarted, and there is no active Fused Location Provider client that has requested location after the services restarted. To avoid this situation you can create a new client and request location updates yourself. For more information, see Receiving Location Updates.

关于java - requestLocationUpdates 与 getLastLocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48488280/

相关文章:

java - Quartz 调度作业异常

java - ws 消息 validator 中的多个 xsd 架构 - 为什么在 schemaCollection 中找不到资源,但在 xsdSchema 中找到资源?

android - 表面支架 : IllegalStateException - Surface was not locked

安卓蓝牙触摸板仿真

java - 如何确定javafx的折线图在监视器上的峰值位置

android - 设置 Android 对话框位置

Java 路径 ImageIcon URL .JAR

java - 是否有任何符合 IEEE754(r) 的 Java 实现?

java - 从 Android Phonegap 插件访问应用程序布局

python - 根据 WiFi 确定笔记本电脑的位置