美文网首页
解决getLastKnownLocation返回null

解决getLastKnownLocation返回null

作者: 严中辉 | 来源:发表于2020-01-07 17:21 被阅读0次

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

    相关文章

      网友评论

          本文标题:解决getLastKnownLocation返回null

          本文链接:https://www.haomeiwen.com/subject/yycdactx.html