As we have already discussed about Requesting Runtime Permission in Marshmallow, now what if we want to give suuport for Runtime Permission in Pre-Marshmallow devices.
And the solution is : Support Library
If we want to use checkSelfPermission() we need to use minSdkVersion 23 but what if we want to give support in both Marshmallow and pre-marshmallow devices. There are small changes we need to do with those methods we are using for Marshmallow.
– ContextCompat.checkSelfPermission()
No matter application is run on Marshmallow or not. This function will correctly return PERMISSION_GRANTED
if the permission is granted. Otherwise PERMISSION_DENIED
will be returned.
– ActivityCompat.requestPermissions()
If this function is called on pre-Marshmallow, OnRequestPermissionsResultCallback will be suddenly called with correct PERMISSION_GRANTED
or PERMISSION_DENIED
result.
-ActivityCompat.shouldShowRequestPermissionRationale()
it will always return false, if called on pre-marshmallow.
Here i am sharing demo code, please take a look at that.
|
int hasReadPermission = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE); if (hasReadPermission != PackageManager.PERMISSION_GRANTED) { showCustomDialog("You need to allow access to External Storage", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 12); } }); } |
We have discussed about Requesting Runtime Permission in last tutorial, but that was for single permission. Today we will discuss about asking for multiple permissions.
Here is some code snippet to take multiple permissions at a time.
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 30
|
List permissionsRequired = new ArrayList(); final List<String> permissionsList = new ArrayList<String>(); if (!checkPermission(permissionsList, Manifest.permission.READ_EXTERNAL_STORAGE)) permissionsRequired.add("Read External Storage"); if (!checkPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE)) permissionsRequired.add("Write External Storage"); if (!checkPermission(permissionsList, Manifest.permission.WRITE_CONTACTS)) permissionsRequired.add("Write Contacts"); if (permissionsList.size() > 0) { if (permissionsRequired.size() > 0) { // Need Rationale String message = "You need to grant access to " + permissionsRequired.get(0); for (int i = 1; i < permissionsRequired.size(); i++) message = message + ", " + permissionsRequired.get(i); showCustomDialog(message, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), 11); } }); return; } requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), 11); return; } |
Continue Reading →
In previous post we have discussed about theory part of Android’s Runtime Permission. Today we will discuss about Requesting Runtime Permission through coding.
As you all know it is Marshmallow functionality so start your new project by setting compileSdkVersion and targetSdkVersion to 23 and mention permission in manifest.xml file.
|
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> |
Now we will check whether READ_EXTERNAL_STORAGE permission is Granted or not with help of checkSelfPermission().
|
int hasReadPermission = checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE); |
Continue Reading →
Android is being keep developed but the latest update to Android M is totally different since there is some major change that would change everything like new Runtime Permission. Surprisingly it is not much talked about in Android Developer community even though it is extremely important and may cause some big trouble in the near future.
Android’s permission system is one of the greatest security concern from the start since those consents are requested at introduce time. Once introduced, the application will have the capacity to get to all of things allowed with no client’s affirmation what precisely application does with the consent.
In Android 6.0 Marshmallow, application will not be granted any permission at installation time. Instead, application has to ask user for a permission one-by-one at runtime.
Please note that permission request dialog not launch automatically. Developer has to call for it manually. In the case that developer try to call some function that requires a permission which user has not granted yet, the function will suddenly throw an Exception which will lead to the application crashing.
Besides, user is also able to revoke the granted permission anytime through phone’s Settings application.

Continue Reading →