美文网首页
解决 locationManager.getLastKnownL

解决 locationManager.getLastKnownL

作者: 克罗克达尔 | 来源:发表于2017-06-09 09:50 被阅读731次
    private Location getLastKnownLocation() {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return null;
            }
            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
            List<String> providers = locationManager.getAllProviders();
            Location bestLocation = null;
            for (String provider : providers) {
    
                Location l = locationManager.getLastKnownLocation(provider);
                if (l == null) {
                    continue;
                }
                if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
                    // Found best last known location: %s", l);
                    bestLocation = l;
                }
            }
            return bestLocation;
        }
    

    相关文章

      网友评论

          本文标题:解决 locationManager.getLastKnownL

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