【问题标题】:requestLocationUpdates vs getLastLocationrequestLocationUpdates 与 getLastLocation
【发布时间】:2018-01-28 15:57:51
【问题描述】:
从 LocationManager 调用 requestLocationUpdates 会更新 getLastLocation 返回的位置吗?
即,我可以这样做吗:
- 调用 getLastLocation。它返回 null,然后:
- 调用requestLocationUpdates;
- 在我的侦听器上接收 onLocationChanged,然后什么都不做
然后,调用 getLastLocation 并得到一些东西!=null?
谢谢!
【问题讨论】:
标签:
java
android
location
locationmanager
【解决方案1】:
如果 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.