Runtime Permission in Pre-Marshmallow
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.
1 2 3 4 5 6 7 8 9 10 11 |
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); } }); } |
Ravi Rupareliya
Latest posts by Ravi Rupareliya (see all)
- Dialogflow Entities With Actions on Google - May 7, 2020
- Actions on Google Using Cloud Functions - February 3, 2020
- Create WhatsApp Stickers Android Application - April 19, 2019