其他分享
首页 > 其他分享> > 解决getLastKnownLocation返回null

解决getLastKnownLocation返回null

作者:互联网

android - getLastKnownLocation returns null - Stack Overflow

LocationManager mLocationManager;
Location myLocation = getLastKnownLocation();

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;
}

标签:返回,mLocationManager,providers,bestLocation,getLastKnownLocation,Location,null
来源: https://www.cnblogs.com/Serendipity2020/p/14263242.html