Requesting Runtime Permission – Part 1
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.
1 |
<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().
1 |
int hasReadPermission = checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE); |
It will give you int value. If permission is given by user, it will return 0 else will return 1. If permission is not given by user we will request for permission at runtime with using below code.
1 2 3 4 |
if (hasReadPermission != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 10); } |
requestPermissions(), This method functions asynchronously: it returns right away, and after the user responds to the dialog box, the system calls the app’s callback method with the results, passing the same request code that the app passed to requestPermissions().
No matter Allow or Deny is chosen, Activity’s onRequestPermissionsResult will always be called to inform a result which we can check from the 3rd parameter, int[] grantResults, like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case 10: if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Permission Granted Toast.makeText(MainActivity.this, "READ_EXTERNAL_STORAGE accept", Toast.LENGTH_SHORT) .show(); } else { // Permission Denied Toast.makeText(MainActivity.this, "READ_EXTERNAL_STORAGE Denied", Toast.LENGTH_SHORT) .show(); } break; default: super.onRequestPermissionsResult(requestCode, permissions, grantResults); } } |
This is how Requesting Runtime Permission works. It’s quite complicated but you have to use it in your application for other permissions like above.
What if user select “Never ask again”
If user denied a permission. In the second launch, user will get a “Never ask again” option to prevent application from asking this permission in the future.
If this option is checked before denying. Next time we call requestPermissions, this dialog will not be appeared for this kind of permission anymore. Instead, it just does nothing.
shouldShowRequestPermissionRationale() will be used to check whether user have selected “Never ask again“. It will return value as True or False, If user have selected Never ask before it will return false, else will return true.
There you can give clerification about “why you need this permission“ and move user to permission setting screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
boolean shouldShow = shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE); if (!shouldShow) { showCustomDialog("You need to allow access to External Storage", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", getPackageName(), null)); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } }); } private void showCustomDialog(String message, DialogInterface.OnClickListener listener) { new AlertDialog.Builder(MainActivity.this) .setMessage(message) .setPositiveButton("OK", listener) .setNegativeButton("Cancel", null) .create() .show(); } |
Here you are done with Requesting Runtime Permission, in next part of this tutorial we will learn How to ask for multiple permissions at a time.
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