Android Requesting Multiple Permission at Runtime in Android 6.0 Marshmallow

In Android 6.0 Marshmallow User required to Grant the Permission At the time of running the Application Not when user is Installing the app.
Mainly There are two types of Permissions-:
  • Normal Permissions
  • Dangerous permissions
In Normal Permissions there is not any risk of user privacy Data. User only define the permissions in the manifest and system Grant the permission Automatically.

In Dangerous Permissions app can access Users's confidential data. So these permission needs the User's Approval before access it.
For More Information See Normal and Dangerous Permissions.

If your Application is running below 6.0 (below SDK 22)You don't need to Approval for Dangerous Permission else you need to Approve the permissions.

How To Check For Permission

If your app needs dangerous permission every time you need to check the permission, you are running the code.


1.Check If your application is Running Above or on Android 6.0 (SDK 23)

1
2
3
4
5
6
7
   if (Build.VERSION.SDK_INT < 23) {
   //Do not need to check the permission 
} else {
    if (checkAndRequestPermissions()) {
      //If you have already permitted the permission
    }
}

2.Create a static final constant to get recognize the request


private static final int MY_PERMISSIONS_REQUEST_ACCOUNTS = 1;


3.Check for the permission

1.For single Permission
private boolean checkAndRequestPermissions() {

//Here i am checking for account Permission 
    int accountPermission = ContextCompat.checkSelfPermission(this, 

Manifest.permission.GET_ACCOUNTS);
    List<String> listPermissionsNeeded = new ArrayList<>();

    if (accountPermission != PackageManager.PERMISSION_GRANTED) {
        listPermissionsNeeded.add(Manifest.permission.GET_ACCOUNTS);
    }
    if (!listPermissionsNeeded.isEmpty()) {
        ActivityCompat.requestPermissions(this, 

listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), MY_PERMISSIONS_REQUEST_ACCOUNTS);
        return false;
    }

    return true;
}
2. For Multiple Permission


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
private boolean checkAndRequestPermissions() {
    int permissionCAMERA = ContextCompat.checkSelfPermission(this,
            Manifest.permission.CAMERA);


    int storagePermission = ContextCompat.checkSelfPermission(this,


            Manifest.permission.READ_EXTERNAL_STORAGE);
   


 List<String> listPermissionsNeeded = new ArrayList<>();
    if (storagePermission != PackageManager.PERMISSION_GRANTED) {
        listPermissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
    }
    if (permissionCAMERA != PackageManager.PERMISSION_GRANTED) {
        listPermissionsNeeded.add(Manifest.permission.CAMERA);
    }
    if (!listPermissionsNeeded.isEmpty()) {
        ActivityCompat.requestPermissions(this, 


     listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), MY_PERMISSIONS_REQUEST_CAMERA);
        return false;
    }

    return true;
}

4.Override this method 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
   @Override    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_ACCOUNTS:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        //Permission Granted Successfully. Write working code here.
                } else {
                   //You did not accept the request can not use the functionality.
                }
                break;
        }
    }

 
5.Enjoy the working code.



Android Requesting Multiple Permission at Runtime in Android 6.0 Marshmallow Android Requesting Multiple Permission at Runtime in Android 6.0 Marshmallow Reviewed by Unknown on 07:13 Rating: 5

13 comments:

  1. Thank you So much. You Save my life and My Job.....
    Love you............

    ReplyDelete
  2. GOOD CODE its not working in 6.0 marshmallow why ..?

    ReplyDelete
    Replies
    1. Change android:targetSdkVersion="23" in AndroidManifest.xml

      Delete
  3. But What if user accepts first and rejects second??

    ReplyDelete
    Replies
    1. Hello Jatin thanks for your question inside overrided onRequestPermissionsResult method you can check for all the requests by its index.
      See here in this method
      @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
      switch (requestCode) {
      case MY_PERMISSIONS_REQUEST_ACCOUNTS:
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

      //Permission Granted Successfully. Write working code here.
      } else {
      //You did not accept the request can not use the functionality.
      }

      if (grantResults.length > 0 && grantResults[1] == PackageManager.PERMISSION_GRANTED) {

      //Permission Granted Successfully. Write working code here.
      } else {
      //You did not accept the request can not use the functionality.
      }
      break;
      }
      }


      Here on every index you can check your permission granted or not.

      Delete
  4. MY_PERMISSIONS_REQUEST_CAMERA or MY_PERMISSIONS_REQUEST_ACCOUNTS ???

    ReplyDelete
  5. I have 4 permissions where i have denied 1st and accepted other
    when i am again accepting denied one ,it is redirecting to else block i.e. did not accept the request

    ReplyDelete
  6. thank you so much..this is the easiest way for multiple permission request!!

    ReplyDelete
  7. You Sir, a life saver... now why can't Google be so clear and concise?

    Their code examples and are over-complicated to the point it's almost incomprehensible...

    ReplyDelete

Powered by Blogger.