Search code examples
javaandroidandroid-studioandroid-6.0-marshmallow

How to ask multiple permissions at the same time in android 6.0+


i want to ask the user to accept the following permissions at the same time(one by one), the permissions are like: checkLocationPermission, checkReadSMS, checkCallingPermission, checkReadState, checkContactWriteState. So, how i can ask all these permissions in my first screen itself. Please help me out in this regard. Thanks in advance.


Solution

  • You have to first check that user phone build version is 23.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        askPermissions(true);
    } else {
        startActivity(new Intent(PermissionsActivity.this, SplashActivity.class));
        finish();
    }
    

    If version is 23 then you need to ask permissions.

    private void askPermissions(boolean isForOpen) {
        isRationale = false;
        List permissionsRequired = new ArrayList();
    
        final List<String> permissionsList = new ArrayList<String>();
        if (!checkPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE))
            permissionsRequired.add("Write External Storage");
        if (!checkPermission(permissionsList, Manifest.permission.CALL_PHONE))
            permissionsRequired.add("Call phone");
        if (!checkPermission(permissionsList, Manifest.permission.READ_PHONE_STATE))
            permissionsRequired.add("Read phone state");
        if (!checkPermission(permissionsList, Manifest.permission.READ_CONTACTS))
            permissionsRequired.add("Read Contacts");
        if (!checkPermission(permissionsList, Manifest.permission.RECEIVE_SMS))
            permissionsRequired.add("Receive SMS");
        if (!checkPermission(permissionsList, Manifest.permission.GET_ACCOUNTS))
            permissionsRequired.add("Get Accounts");
        if (!checkPermission(permissionsList, Manifest.permission.ACCESS_COARSE_LOCATION))
            permissionsRequired.add("Location");
        if (!checkPermission(permissionsList, Manifest.permission.ACCESS_FINE_LOCATION))
            permissionsRequired.add("Location");
    
        if (permissionsList.size() > 0 && !isRationale) {
            if (permissionsRequired.size() > 0) {
    
            }
            if (isForOpen) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    ActivityCompat.requestPermissions(this, permissionsList.toArray(new String[permissionsList.size()]),
                            11);
                }
            }
    
        } else if (isRationale) {
            if (isForOpen) {
    
                new android.support.v7.app.AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle)
                        .setTitle("Permission Alert")
                        .setMessage("You need to grant permissions manually. Go to permission and grant all permissions.")
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                Uri uri = Uri.fromParts("package", getPackageName(), null);
                                intent.setData(uri);
                                startActivityForResult(intent, 123);
                            }
                        })
                        .show();
            }
        } else {
            startActivity(new Intent(PermissionsActivity.this, SplashActivity.class));
            finish();
        }
    }
    
    private boolean checkPermission(List permissionsList, String permission) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                permissionsList.add(permission);
                // Check for Rationale Option
                if (!isFirst) {
                    if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
                        isRationale = true;
                        return false;
                    }
                }
            }
        }
        return true;
    }
    

    on the onRequestPermissionsResult you need to check which permissions granted

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 11:
                Map<String, Integer> perms = new HashMap<String, Integer>();
                // Initial
                perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
                perms.put(Manifest.permission.CALL_PHONE, PackageManager.PERMISSION_GRANTED);
                perms.put(Manifest.permission.READ_PHONE_STATE, PackageManager.PERMISSION_GRANTED);
                perms.put(Manifest.permission.READ_CONTACTS, PackageManager.PERMISSION_GRANTED);
                perms.put(Manifest.permission.RECEIVE_SMS, PackageManager.PERMISSION_GRANTED);
                // Fill with results
                for (int i = 0; i < permissions.length; i++) {
                    perms.put(permissions[i], grantResults[i]);
                }
                // Check for ACCESS_FINE_LOCATION
                if (perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
                        perms.get(Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED &&
                        perms.get(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED &&
                        perms.get(Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED &&
                        perms.get(Manifest.permission.RECEIVE_SMS) == PackageManager.PERMISSION_GRANTED) {
                    // All Permissions Granted
                    startActivity(new Intent(PermissionsActivity.this, SplashActivity.class));
                    finish();
                } else {
                    // Permission Denied
                    Toast.makeText(this, "Some Permission is Denied.", Toast.LENGTH_SHORT)
                            .show();
                    isFirst = false;
                    askPermissions(true);
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    
        }
    }
    

    If user has set the permission to never ask again then the application setting screen will open. User will allow/Deny permission there. You need to check again on the activityResult.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        askPermissions(true);
    }