0

I want to grant multiple permissions at once..... This is my code.. this is just sample I want to Give Multiple permissions at once..

public class MainActivity extends Activity {

public static final int R_PERM = 123;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.data);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    if ((CheckPermission(this, Manifest.permission.CAMERA))&&
         (CheckPermission(this, Manifest.permission.READ_PHONE_STATE))&&
         (CheckPermission(this, Manifest.permission.NFC)))
    {
        PermHandling();
    }

    else {
        RequestPermission(MainActivity.this, Manifest.permission.CAMERA, R_PERM);
        RequestPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE, R_PERM );
        RequestPermission(MainActivity.this, Manifest.permission.NFC, R_PERM );

        //NewPermHandling();
    }

}
private void PermHandling() {
    //My app internal parts....
    //Here my stuff works...
}

//private void NewPermHandling(){

//}

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

    switch (permsRequestCode) {

        case R_PERM: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                PermHandling();
            } else {
                //Toast.makeText(this, "Please Grant Permissions other wise app will close.!", Toast.LENGTH_SHORT).show();
            }
            return;
        }
    }
}

public void RequestPermission(Activity thisActivity, String Permission, int Code) {
    if (ContextCompat.checkSelfPermission(thisActivity,
            Permission)
            != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                Permission)) {
        } else {
            ActivityCompat.requestPermissions(thisActivity,
                    new String[]{Permission},
                    Code);
        }
    }
}

public boolean CheckPermission(Context context, String Permission) {
    if (ContextCompat.checkSelfPermission(context,
            Permission) == PackageManager.PERMISSION_GRANTED) {
        return true;
    } else {
        return false;
    }
}

}

Here I have Given permissions one by one So I am getting Permissions one by one at Run time Please suggest me to Give all permissions at once..

Please suggest me on my code where should I need to change..

here I my App I have more permissions I need to give at once like camera,location,storage etc... Please suggest me on Mycode....

Update and all permissions should be at once with Never Ask again check button....

Please suggest me of this kind in my code,,,,

Don't Be negative
  • 1,215
  • 3
  • 19
  • 46

2 Answers2

-1

Inside your Request Permision method instead of sending one permission at a time you can ask for multiple using.

     ActivityCompat.requestPermissions(thisActivity,
                new String[]{Permission1,Permission2,Permission3},
                Code);

Where Permission1=Manifest.permission.CAMERA Permission2= Manifest.permission.READ_PHONE_STATE Permission3=Manifest.permission.NFC

What this does is it makes android ask for permissions one after the other in dialog boxes. And you can check which permissions are granted all at once in your onRequestPermissionsResult by running through the int[] grantResults

Note that int[] grantResults returns permission grant result in the order of permissions asked. In our case - Manifest.permission.CAMERA ,Manifest.permission.READ_PHONE_STATE, Manifest.permission.NFC

Have a look at link below where it has been explained in detail.

https://developer.android.com/training/permissions/requesting.html

Ragesh Ramesh
  • 3,470
  • 2
  • 14
  • 20
-1

You can create permission array and pass it directly to requestPermissions().

String[] PERMISSIONS = new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.RECORD_AUDIO};

and pass it to requestPermissions()

requestPermissions(PERMISSIONS, 10);

and check whether you got all permission or not in onRequestPermissionsResult

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

    switch (requestCode) {
        case 10:
            if (hasAllPermissionsGranted(grantResults)) {
                // Permission Granted

            } else {
                // Permission Denied

            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

Here is hasAllPermissionsGranted method

public boolean hasAllPermissionsGranted(@NonNull int[] grantResults) {
    for (int grantResult : grantResults) {
        if (grantResult == PackageManager.PERMISSION_DENIED) {
            return false;
        }
    }
    return true;
}

For another approach to take care of multiple permission, check Requesting Runtime Permission – Part 2

Ravi
  • 34,851
  • 21
  • 122
  • 183
  • Yeah sure, mail me your project, i will complete all your task :) We are here to help you, not to complete your project. We can give you code snippets but not whole code. – Ravi May 27 '16 at 06:14
  • The Above one is my code sir... Please suggest me sir... My Project is Huge... and confidential... I am just one part of it,,, still I don't have of full code... sorry sir.... Please help me – Don't Be negative May 27 '16 at 06:15
  • @downvoter : thank you, but care to mention my mistake. – Ravi Jun 01 '16 at 10:42