GPS getLastKnownLocation报错问题

因为getLastKnownLocation不一定每次都能获取到,所以不能直接mLocationManager.getLastKnownLocation(provider);
不记得看了哪个大牛的博客了,想起来要记录的时候已经找不到链接了,请原创作者见谅
用下面方法可行,自用记录

private Location getLastKnownLocation() {
    List providers = mLocationManager.getProviders(true);
    Location bestLocation = null;
    for (String provider : providers) {
        @SuppressLint("MissingPermission")
        Location l = mLocationManager.getLastKnownLocation(provider);
        if (l == null) {
            continue;
        }
        if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
            bestLocation = l;
        }
    }
    return bestLocation;
}

你可能感兴趣的:(GPS getLastKnownLocation报错问题)