1

I am trying to request permissions on my Launcher Activity. For API < 23, it works perfect. However, when I test the app on a device running API 23, it says: "PostPaid Balance has stopped." I hit the "close App button," the app closes and immediately asks for one permission. I hit accept. Then I tap on the app icon to reopen and the same thing happens, except that now it asks for the next permission. Then I tap on the app icon and this time executes correctly. It seems like it is asking for permissions one at a time. Any ideas on how to go about this?

// Below code is implemented on onCreate() of the launcher activity.
 if (Build.VERSION.SDK_INT < 23) {
        ActivityCompat.checkSelfPermission(this.getApplicationContext(), "android.permission.READ_SMS");
        ActivityCompat.checkSelfPermission(this.getApplicationContext(), Manifest.permission.READ_CALL_LOG);
        ActivityCompat.checkSelfPermission(this.getApplicationContext(), Manifest.permission.READ_PHONE_STATE);

        if ((ActivityCompat.checkSelfPermission(this, "android.permission.READ_SMS") != PackageManager.PERMISSION_GRANTED)) {
            requestSmsPermission();
        }

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            requestPhoneStatePermission();
        }

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
            requestCallLogPermission();
        }
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if ((this.checkSelfPermission("android.permission.READ_SMS") != PackageManager.PERMISSION_GRANTED) &&
                (this.checkSelfPermission(Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) &&
                (this.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)) {
            this.requestPermissions(new String[]{"android.permission.READ_SMS", Manifest.permission_group.PHONE}, REQUEST_SMS);

        }
    }

enter image description here

Gerardo Soriano
  • 69
  • 1
  • 10
  • 1
    Request all three permissions in a single `requestPermissions()` call. Also, it is unclear why you have your code set up to do different things based on API level. `ActivityCompat` and `ContextCompat` are backwards-compatible. – CommonsWare Oct 01 '16 at 19:06
  • I am setting up the code to do different things because certain features are not going to be available for API < 23. – Gerardo Soriano Oct 01 '16 at 19:35
  • What would you recommend? Should I only ask for permissions if the API is 23 and disregard the if(API < 23) ? – Gerardo Soriano Oct 01 '16 at 19:36
  • "I am setting up the code to do different things because certain features are not going to be available for API < 23" -- ah, OK. Your current code does not show that. "Should I only ask for permissions if the API is 23 and disregard the if(API < 23) ?" -- well, you know that there are no runtime permissions < 23, so there's little point in running through code asking for them. – CommonsWare Oct 01 '16 at 19:38
  • yes, you are right! I completely forgot about permissions in API < 23. – Gerardo Soriano Oct 01 '16 at 19:39
  • Another work around for this is setting the target api level (not minimum sdk nor buildTool) below 22. This makes you totally free from this issue, but it is a hack that is not recommended. – March3April4 Oct 17 '16 at 14:36
  • Possible duplicate of [How to check the multiple permission at single request in Android M?](https://stackoverflow.com/questions/34040355/how-to-check-the-multiple-permission-at-single-request-in-android-m) – Flimm Nov 02 '18 at 12:27

1 Answers1

2

To answer your question on how to request for multiple permissions in one go, add permissions to your array of strings. In this example, I want to request permissions for READ_PHONE_STATE and WRITE_EXTERNAL_STORAGE:

ArrayList<String> arrPerm = new ArrayList<>();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
    arrPerm.add(Manifest.permission.READ_PHONE_STATE);
}
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    arrPerm.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if(!arrPerm.isEmpty()) {
    String[] permissions = new String[arrPerm.size()];
    permissions = arrPerm.toArray(permissions);
    ActivityCompat.requestPermissions(this, permissions, MY_PERMISSIONS_REQUEST);
}

Now, check which permissions were granted:

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

    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0) {
                for(int i = 0; i < grantResults.length; i++) {
                    String permission = permissions[i];
                    if(Manifest.permission.READ_PHONE_STATE.equals(permission)) {
                        if(grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                            // you now have permission
                        }
                    }
                    if(Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permission)) {
                        if(grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                            // you now have permission
                        }
                    }
                }
            } else {
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            break;
        }
    }

    // other 'case' lines to check for other
    // permissions this app might request
}
user1506104
  • 6,554
  • 4
  • 71
  • 89