有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java requestLocationUpdates vs getLastLocation

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

我可以这样做吗

  1. 调用getLastLocation。它返回null,然后:
  2. 调用RequestLocationUpdate
  3. 在我的侦听器上仅接收已更改的位置,而不执行任何操作

在那之后,打电话给getLastLocation获取一些信息=空的

谢谢


共 (1) 个答案

  1. # 1 楼答案

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

    RequestLocationUpdate将请求正在进行的位置更新,直到您取消订阅位置更新

    更多详细信息可在此处找到:

    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.