Problem in catching latitude and longitude android 5

2

My code does not handle location in android 5,

 public void pegaLocalizacao() {
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    latitude =  location.getLatitude();
    longitude = location.getLongitude();
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    }
    locListener = new LocationListener() {
        public void onLocationChanged(Location location) {


        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }

    };

    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 30000, 0, locListener);


}
    
asked by anonymous 06.10.2016 / 21:36

2 answers

1

Try this:

private void checkPermission() {
    // Verifica necessidade de verificacao de permissao
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // Verifica necessidade de explicar necessidade da permissao
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
            Toast.makeText(this, "E necessario permitir o uso da localização", Toast.LENGTH_SHORT).show();
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    CODE_PERMISSION_LOCATION);
        } else {
            // Solicita permissao
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    CODE_PERMISSION_LOCATION);
        }
    }
}
    
06.10.2016 / 22:14
1

Try this:

 public void pegaLocalizacao() {

    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            // Called when a new location is found by the network location provider.
            try {
                List<Address> geocodeMatches = new Geocoder(MainActivity.this).getFromLocation(location.getLatitude(), location.getLongitude(),1);
                geocodeMatches.get(0);

                double latidute = location.getLatitude();
                double longitude = location.getLongitude();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onProviderDisabled(String provider) {
        }
    };

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    } else {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }
}
    
06.10.2016 / 21:52