Continue running after accepting the Android Permission

0

I'm doing a test using Google Maps. It's working but it's this way. Once the app is started, it checks the permission by calling the Permission class. If the user accepts, the app starts and if it refuses the app to close with a message stating that it is necessary to accept the permissions. That's fine, but if he accepts the permission the app only works if it closes and reopens the app or if it flips the screen.

Basically the app takes the current location of the device and shows it on the map. I would like it as soon as it chooses to allow, the app already worked without the details I mentioned above.

   private GoogleMap mMap;
    private Marker marcador;
    double lat = 0.0;
    double lng = 0.0;
    private String[] permissoesNecessarias = new String[]{
            android.Manifest.permission.ACCESS_FINE_LOCATION,
            android.Manifest.permission.ACCESS_COARSE_LOCATION
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        Permissao.validaPermissoes(1, this, permissoesNecessarias);

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        minhaLocalizacao();

}
    private void adicionarMarcador(double lat, double lng) {
        LatLng coordenadas = new LatLng(lat, lng);
        CameraUpdate minhaLocalizacao = CameraUpdateFactory.newLatLngZoom(coordenadas, 16);

        if (marcador != null) marcador.remove();
        marcador = mMap.addMarker(new MarkerOptions()
                .position(coordenadas)
                .title("Você está aqui")
                .icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher_round)));
        mMap.animateCamera(minhaLocalizacao);
    }

    private void atualizarLocalizacao(Location location) {

        if (location != null) {

            lat = location.getLatitude();
            lng = location.getLongitude();
            adicionarMarcador(lat, lng);
        }
    }

    LocationListener locListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {

           atualizarLocalizacao(location);
        }

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

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }
    };


  private void minhaLocalizacao() {

        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
       if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
                PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) ==
                        PackageManager.PERMISSION_GRANTED) {
}

           Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
           atualizarLocalizacao(location);
           locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, locListener);
       }

    }

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        for ( int resultado : grantResults){
            if ( resultado == PackageManager.PERMISSION_DENIED){
                alertaValidacaoPermissao();
            }
        }
    }

    private void alertaValidacaoPermissao() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Permissão");
        builder.setMessage("Para usar o app você precisa aceitar as permissões ");
        builder.setPositiveButton("Confirmar", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                finish();
            }
        });
        builder.create();
        builder.show();
    }

}
    
asked by anonymous 06.07.2017 / 21:00

1 answer

1

In your onRequestPermissionsResult method, you have not handled his case if he has accepted the permissions.

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        for ( int resultado : grantResults){
            if ( resultado == PackageManager.PERMISSION_DENIED){
                alertaValidacaoPermissao();
                return;
            }
        }
        minhaLocalizacao();
    }
    
06.07.2017 / 21:16