getLastKnownLocation()返回null [英] getLastKnownLocation() returns null
问题描述
在这里我已经阅读了很多关于这个主题的Q& As,但我不得不说它们都不起作用。
我的问题是,即使启用了GPS,我也无法获取位置,除非我打开Google地图并找到我的位置,然后返回到应用程序,
我有以下函数来获取位置。
<$ p $公共位置getCurrentLocation(){
LocationManager locationManager =(LocationManager)上下文
.getSystemService(Context.LOCATION_SERVICE);
标准标准=新标准();
String provider = locationManager.getBestProvider(criteria,true);
位置myLocation = locationManager.getLastKnownLocation(provider);
返回myLocation;
}
有什么我在如何解决这个问题上失踪?
我也试过这个
http: //developer.android.com/training/location/retrieve-current.html#last-known
,但仍然返回空值。
谢谢提前
有什么我错过如何解决这个问题?
GPS无线电正常关机,因为它们是主要的电量消耗。因此, getLastKnownLocation()
经常会返回 null
或旧的位置,因为没有任何内容正在检查位置修正。因此, getLastKnownLocation()
只有在您对该位置有兴趣时才有用,并且如果没有位置则很高兴。
如果您需要知道位置,您需要使用 requestLocationUpdates()
或类似的东西,以使Android启动GPS收音机并主动尝试找到位置。
I have read a lot of Q&As on this topic here on SO but I have to say that none of them works.
My problem is that, even though I have GPS enabled, I cannot get a location unless I open Google Maps and get my location and then go back to the app, which is definitely not an option for the users.
I have the following function to get the location.
public Location getCurrentLocation() {
LocationManager locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location myLocation = locationManager.getLastKnownLocation(provider);
return myLocation;
}
Is there anything I'm missing on how to solve this? I have also tried this http://developer.android.com/training/location/retrieve-current.html#last-known but still returns null.
Thanks in advance
Is there anything I'm missing on how to solve this?
GPS radios are powered down normally, as they are major battery drain. Hence, getLastKnownLocation()
can frequently return null
or a stale location, because nothing is checking for location fixes. getLastKnownLocation()
, therefore, is only useful if you have a casual interest in the location and are happy if there is no location.
If you need to know the location, you will need to use requestLocationUpdates()
or similar stuff, to cause Android to power on the GPS radio and actively try to find the location.
这篇关于getLastKnownLocation()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!