gpt4 book ai didi

java - requestSingleUpdate(...) 不会自动获取 GPS 位置

转载 作者:行者123 更新时间:2023-11-29 07:14:58 25 4
gpt4 key购买 nike

如果我们想在 Android 中获得单个 GPS 定位,我们实际上会使用带有 LocationManager.GPS_PROVIDER 的已知函数 requestSingleUpdate(),对吗?

现在我在我的函数 getLocation() 中执行此操作并打开了 GPS,但设备不会自动建立连接,除非我手动关闭 GPS 然后再次打开它。网络提供商的 requestSingleUpdate() 工作完美,我只是不知道为什么这不适用于 GPS 修复。

这是我的源代码的一部分:

getLocation() 由另一个对象每 2 分钟运行一次。在 getLocation() 中,如果 GPS 和网络已打开,它会尝试在第一分钟内获取 GPS 定位,然后获取网络位置,如果没有可用的 GPS 定位。

public boolean getLocation() {
// DEBUG
Log.d("GPS Connection", "Entering getLocation()");
gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if(!gpsEnabled && !networkEnabled)
return false;

if(gpsEnabled && networkEnabled) {
// DEBUG
Log.d("GPS Connection", "Request GPS Data");
locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, gpsLocationListener, Looper.getMainLooper());
gpsGetLocationTimeout = new Timer();
gpsGetLocationTimeout.schedule(new GetNetworkLocation(), GPS_MAX_CONNECTION_TIME_MS);
}

...

最佳答案

来自Android documentation:

It may take a while to receive the most recent location. If an immediate location is required, applications may use the getLastKnownLocation(String) method. In case the provider is disabled by the user, the update will not be received, and the onProviderDisabled(String) method will be called. As soon as the provider is enabled again, the onProviderEnabled(String) method will be called and location updates will start again.

GPS 可能需要很长时间才能冷启动。如果您在室内、 parking 场、被高楼环绕、手机质量很差,或者 GPS 卫星大神心情不好,您可能永远无法获得 GPS。

在某些应用程序中,我们会做一些事情,比如请求 GPS 位置,然后如果它在 60 秒后仍未到达,则取消请求,这样我们就不会在 GPS RX 半永久开启的情况下耗尽手机电池.

您可以请求最后一个已知位置并检查其使用年限并确定它是否足够新以供使用。网络定位快速且便宜(就电池使用而言),一个好的策略是在获得 GPS 之前使用网络定位。

我在 Android 中编写 GPS 代码的经验是,GPS 会做它想做的事情,这在很多方面都不是文档引导您期望的事情。 GPS 需要被视为可能可用或不可用的东西,您的程序逻辑可以设计为处理该问题。

关于java - requestSingleUpdate(...) 不会自动获取 GPS 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10405277/

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