gpt4 book ai didi

java - requestLocationUpdates 与 getLastLocation

转载 作者:行者123 更新时间:2023-12-02 11:41:46 26 4
gpt4 key购买 nike

从 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com