我正在设备上测试我的 Android 应用程序,但在使用时它总是失败
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
但是,如果我使用 NETWORK_PROVIDER 而不是 GPS_PROVIDER,它就可以工作。 currentLocation 返回 null。
我该如何处理这个问题?我需要在 locationListener 中添加什么具体内容吗?
谢谢!
最佳答案
GPS_PROVIDER 可能无法在有限的地方工作,假设在房子内或类似的地方。您应该以这样的方式对其进行编程:如果 GPS 提供商给出空位置,则您可以使用网络提供商作为替代方案,尽管其准确性不是那么高。 这里还有一个问题。您的代码应如下所示:
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
关于java - Android 中 LocationManager 的位置返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7188105/