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-:
Mainly There are two types of Permissions-:
- Normal Permissions
- Dangerous permissions
In Dangerous Permissions app can access Users's confidential data. So these permission needs the User's Approval before access it.
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.
2.Create a static final constant to get recognize the request
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 } } |
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
Reviewed by Unknown
on
07:13
Rating:
Thank you So much. You Save my life and My Job.....
ReplyDeleteLove you............
Your welcome
DeleteGOOD CODE its not working in 6.0 marshmallow why ..?
ReplyDeleteChange android:targetSdkVersion="23" in AndroidManifest.xml
DeleteFabolous Job...
ReplyDeleteNice work...thanks
ReplyDeleteWelcome
DeleteBut What if user accepts first and rejects second??
ReplyDeleteHello Jatin thanks for your question inside overrided onRequestPermissionsResult method you can check for all the requests by its index.
DeleteSee 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.
MY_PERMISSIONS_REQUEST_CAMERA or MY_PERMISSIONS_REQUEST_ACCOUNTS ???
ReplyDeleteI have 4 permissions where i have denied 1st and accepted other
ReplyDeletewhen i am again accepting denied one ,it is redirecting to else block i.e. did not accept the request
thank you so much..this is the easiest way for multiple permission request!!
ReplyDeleteYou Sir, a life saver... now why can't Google be so clear and concise?
ReplyDeleteTheir code examples and are over-complicated to the point it's almost incomprehensible...