0

I need to request permission to send SMS and to read contacts.

I've managed to make the android ask permission to send SMS with the following code:

        if(ContextCompat.checkSelfPermission(MainActivity.this,
            Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
        if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                Manifest.permission.SEND_SMS)){
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.SEND_SMS},1);
        } else {
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.SEND_SMS},1);
        }
    } else {
        // do nothing
    }

I've tried to add code above with READ_CONTACT instead of SEND_SMS but I need to open the app twice for it to ask the second permission (READ_CONTACT)

I've also tried to change to the code:

    if((ContextCompat.checkSelfPermission(MainActivity.this,
        Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(MainActivity.this,
        Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) ) {
    if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
            Manifest.permission.SEND_SMS) && ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
            Manifest.permission.READ_CONTACTS)){
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.SEND_SMS},1);
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.READ_CONTACTS},1);

    } else {
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.SEND_SMS},1);
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.READ_CONTACTS},1);
    }
} else {
    // do nothing
} 

I know it looks messy and it didn't work as well.

Thank you in advance for anyone willing to help

João Vieira
  • 121
  • 1
  • 9
  • 3
    `requestPermissions()` takes an array of permissions, not just one. So, call `requestPermissions()` once with both `SEND_SMS` and `READ_CONTACTS`. – CommonsWare May 07 '18 at 19:14
  • Good point, how about the conditions? should I keep them as it shows on the second part? – João Vieira May 07 '18 at 19:17
  • Probably not. You're starting to get into more complex territory, where there are more combinations (you have neither permission, you only have `SEND_SMS`, you only have `READ_CONTACTS`, you have both). [This sample app](https://github.com/commonsguy/cw-omnibus/tree/v8.11/Camera/CameraKit) shows an `AbstractPermissionActivity` that handles requesting N permissions up front when an activity is started. And there are [plenty of libraries to give you a simpler permission API](https://android-arsenal.com/tag/235?sort=created) that you might consider investigating. – CommonsWare May 07 '18 at 19:22

0 Answers0