0

I'm working in an Android app, and I have declared the method requestAppPermissions in my Activity.

Well, what happened with this code, for some reason, it's requesting the first one (in this case, location) and skip the others; but, I move some other permission to the first if the method is requesting the first one, skipping the others.

What I have wrong with my code?

public void requestAppPermissions(){
    if (Build.VERSION.SDK_INT >= 21) {
        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION)) {
                //El usuario denegó los permisos con el botón "Don't show again!!!"
                startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + BuildConfig.APPLICATION_ID)));
            } else {
                //Se solicita el permiso al usuario
                ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            }
        }else{
            LocationManager lm = (LocationManager) this.getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
            Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            if(location != null){
                Double latitude = location.getLatitude();
                Double longitude = location.getLongitude();
                //new MapFunctions().RecenterMap(googleMap, new LatLng(latitude, longitude), 14, "");
                LOCATION_CENTER = new LatLng(latitude, longitude);
                // Toast.makeText(MainActivity.this, "latitude: " + latitude + " - longitude" + longitude, Toast.LENGTH_LONG).show();
            }
        }

        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.CAMERA)) {
                startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + BuildConfig.APPLICATION_ID)));
            } else {
                ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CAMERA}, 2);
            }
        }

        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + BuildConfig.APPLICATION_ID)));
            } else {
                ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 3);
            }
        }

        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.READ_EXTERNAL_STORAGE)) {
                startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + BuildConfig.APPLICATION_ID)));
            } else {
                ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 4);
            }
        }

    }
}




@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.
                Toast.makeText(NSActivity.this, "Feature enabled", Toast.LENGTH_SHORT).show();
            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                Toast.makeText(NSActivity.this, "Feature disabled", Toast.LENGTH_SHORT).show();
            }
            return;
        }

        case 2: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                Toast.makeText(NSActivity.this, "Feature enabled", Toast.LENGTH_SHORT).show();
            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                Toast.makeText(NSActivity.this, "Feature disabled", Toast.LENGTH_SHORT).show();
            }
            return;
        }

        case 3: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.
                Toast.makeText(NSActivity.this, "Feature enabled", Toast.LENGTH_SHORT).show();
            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                Toast.makeText(NSActivity.this, "Feature disabled", Toast.LENGTH_SHORT).show();
            }
            return;
        }

        case 4: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.
                Toast.makeText(NSActivity.this, "Feature enabled", Toast.LENGTH_SHORT).show();
            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                Toast.makeText(NSActivity.this, "Feature disabled", Toast.LENGTH_SHORT).show();
            }
            return;
        }

    }
}
Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
  • 2
    You need to stack them togheter in a single request. You should check the state of each permission, store it in a string array if the permission has not been provided, then at the end you can send a single request with the array containing all the permissions you need. Read here: https://stackoverflow.com/a/34343101/2910520 – MatPag Jul 27 '17 at 14:20

1 Answers1

0

Something similar to this could be helpfull...

String[] neededPermissions = new String[]{
    android.Manifest.permission.ACCESS_FINE_LOCATION,
    android.Manifest.permission.CAMERA,
    android.Manifest.permission.WRITE_EXTERNAL_STORAGE //if you can write, you can read
}

public static List<String> getPermissionsRequest(@NonNull Context context, 
                                                 @NonNull String[] permissions){
    List<String> permissionsToRequest = new ArrayList<>();
    for (int i = 0; i < permissions.length; i++){
        if (ContextCompat.checkSelfPermission(context, permissions[i]) 
                  != PackageManager.PERMISSION_GRANTED){
            permissionsToRequest.add(permissions[i]);
        }
    }
    return permissionsToRequest;
}

//now in the onCreate of your Activity you can do something like this:
List<String> permissionsToRequest = getPermissionsRequest(this, neededPermissions);
//if permissionsToRequest size is > 0 you need some more permission(s)
if (permissionsToRequest.size() > 0){
    ActivityCompat.requestPermissions(MainActivity.this,
         permissionsToRequest.toArray(new String[0]), PERMISSION_ALL_REQUEST_CODE);
}

//now in the onRequestPermissionsResult you can handle everything
//as always and proceeed if everything is ok

i've not tested this currently but should work

MatPag
  • 41,742
  • 14
  • 105
  • 114