Android中LocationManager的位置返回null
我正在设备上测试我的 Android 应用程序,当我使用时它总是失败
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
但是,如果我使用 NETWORK_PROVIDER 而不是 GPS_PROVIDER,它就可以工作。当前位置返回 null。
我该如何处理这个问题?我需要在 locationListener 中添加什么具体内容吗?
谢谢!
I'm testing my Android app on a device, and it always fails when I use
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
However, if I use NETWORK_PROVIDER instead of GPS_PROVIDER, it works. The currentLocation returns null.
How can I handle this? Is there anything specific I need to put in my locationListener?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
GPS_PROVIDER 可能无法在有界的地方工作,假设在房子内或类似的地方。您应该以这样的方式对其进行编程:如果 GPS 提供商给出空位置,则您可以使用网络提供商作为替代方案,尽管其准确性不是那么高。
这里还有一个问题。您的代码应如下所示:
GPS_PROVIDER may not work in bounded place, suppose inside a house or something like this. You should program it in such a way that if gps provider gives null location then you use network provider as a alternative although its accuracy is not so high.
Here there is another issue. Your code should be as follows:
首先,检查设备状态栏上的GPS图标是否已停止闪烁,这表明您确实已锁定GPS。
如果当前位置未知(例如,如果您缺少 GPS 锁定),则该调用将返回 null。
First of all, check that the GPS icon on device status bar has stopped blinking, marking you actually have GPS lock.
The call returns null if the location is not currently known, for example if you lack GPS lock.