Android原生定位gps为什么有些设备location一直为null,而虚拟机却很快就有定位?

getLastKnownLocation获取一直为null,或者突然有数据了,重新开机又没有了,有什么办法能缓存这个数据或者开机就有定位吗

 

getLastKnownLocation 这个方法获取的是上一次缓存的位置信息,所以当你没有缓存位置信息时得到的结果就会是 null 。
  1. private Location getLastKnownLocation() {

  2. mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);

  3. List<String> providers = mLocationManager.getProviders(true);

  4. Location bestLocation = null;

  5. for (String provider : providers) {

  6. Location l = mLocationManager.getLastKnownLocation(provider);

  7. if (l == null) {

  8. continue;

  9. }

  10. if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {

  11. // Found best last known location: %s", l);

  12. bestLocation = l;

  13. }

  14. }

  15. return bestLocation;

  16. }

  17. 希望这段代码能帮助到你