geLocations() 始终返回 null
我从 GPS 或网络获取位置。问题是我已经在 HTC Desire HD 上进行了测试,它工作得很好,但现在我开始在三星 Galaxy 位置的其他设备上进行测试,结果始终为空。
这是我获取位置的代码
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if( currentLocation == null){
currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
提前致谢
I'm getting the locations either from GPS or Network. The problem is that i have tested it on htc desire hd and it worked perfectly fine, but now i started testing on other devices in samsung galaxy locations are always null.
Here is my code for getting locations
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if( currentLocation == null){
currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有任何可以使用 GPS 的 API 示例吗?我认为如果应用程序已经设置了位置侦听器并等待第一个位置到达,某些手机只会向应用程序提供最后一个已知位置。你的代码不会这样做。
Have you got any of the API examples for GPS to work? I presume some phones will only give the app the last known location if the app has already set up a location listener, and waited for the first location to arrive. Your code wouldn't do this.
您将需要注册一个位置更改监听器。添加如下内容:
确保在请求位置更新时添加正确的等待时间。如果使用 0 而不是 1000,系统将没有足够的时间来获取新位置。
运行上述代码后调用 getLastKnownLocation() 应该可以消除 null 问题。
You will need to register a listener for location changes. Add something like the following:
Make sure you add the proper wait time when requesting location updates. With a zero instead of the 1000, the system will not have enough time to get the new location.
A call to getLastKnownLocation() after running the above code should remove your null problem.