LocationManager的位置,在Android中返回null [英] LocationManager's location in Android returns null
问题描述
我在测试设备上我的Android应用程序,当我用它总是失败
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);
不过,如果我用NETWORK_PROVIDER代替GPS_PROVIDER,它的工作原理。该currentLocation返回null。
However, if I use NETWORK_PROVIDER instead of GPS_PROVIDER, it works. The currentLocation returns null.
我该如何处理呢?有没有什么具体的,我需要把我的LocationListener的?
How can I handle this? Is there anything specific I need to put in my locationListener?
谢谢!
推荐答案
GPS_PROVIDER可能无法在有限的地方工作,想在房子里或类似这样的东西。如果GPS提供商给空位置,然后使用网络供应商作为一个替代方案,虽然其准确度不那么高,您应该编写一次以这样的方式。 这里还有另外一个问题。您的code应该如下:
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:
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
这篇关于LocationManager的位置,在Android中返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!