getLastKnownLocation获取一直为null,或者突然有数据了,重新开机又没有了,有什么办法能缓存这个数据或者开机就有定位吗
getLastKnownLocation 这个方法获取的是上一次缓存的位置信息,所以当你没有缓存位置信息时得到的结果就会是 null 。
private Location getLastKnownLocation() {
mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
Location l = mLocationManager.getLastKnownLocation(provider);
if (l == null) {
continue;
}
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
// Found best last known location: %s", l);
bestLocation = l;
}
}
return bestLocation;
}
希望这段代码能帮助到你