androidandroid-permissionsruntime-permissions

requestPermissions is called multiple times, but why does the dialog only appear once?


    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.SEND_SMS},
            1);
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
            2);
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.CALL_PHONE},
            3);

Here, I have the three permissions I want. However, when the app pops up only one of the three permissions pops up. How do I get all three permissions to request?


Solution

  • It is an asynchronous call and there are possibly many calls. Hence, there should be a key to let you distinct between them. You will use it in

    @Override
    public void onRequestPermissionsResult(int requestCode,
            String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
                // Your code here
            }
        }
    }
    

    In this case MY_PERMISSIONS_REQUEST_READ_CONTACTS is an int value and you are free to define it. For instance:

    int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 999;