我在这里阅读了很多关于这个主题的问答,但我不得不说它们都不起作用。
我的问题是,即使我启用了 GPS,我也无法获取位置,除非我打开 Google 地图并获取我的位置,然后返回到应用程序,这绝对不是用户的选择。
我有以下函数来获取位置。
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;
}
关于如何解决这个问题,我有什么遗漏的吗?
我也尝试过这个http://developer.android.com/training/location/retrieve-current.html#last-known http://developer.android.com/training/location/retrieve-current.html#last-known但仍然返回 null。
提前致谢
关于如何解决这个问题,我有什么遗漏的吗?
GPS 无线电通常会断电,因为它们是主要的电池消耗者。因此,getLastKnownLocation()
可以经常回来null
或过时的位置,因为没有任何东西检查位置修复。getLastKnownLocation()
因此,只有当您对某个位置有偶然的兴趣并且对没有位置感到高兴时,此选项才有用。
如果您需要知道位置,则需要使用requestLocationUpdates()
或类似的东西,让 Android 打开 GPS 无线电并主动尝试查找位置。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)