Android Request permission dynamically example

In android if you are using target version less than 23 you can mention permission directly in your AndroidManifest.xml file. But after the release of version 23 android is giving the choice to user .That means if the user allows only our application can make use of certain functionalities in his phone. So developers need to do some extra coding to request the permission which is required to run the application as well as the code needs to handle the response given by user. In this tutorial we will learn how to get run time permission in android.

The tutorial will explain small codes which is used for checking whether the permission is already granted , how to request permission and how to handle the result and all three functionalities of handling multiple request as well.

Contents

  • Check permission is already granted
  • Request for permission
  • Handling the result based on users decision
  • Manage multiple requests
permission
permission

Note : Here i am trying to  get READ_CONTACTS permission which is required for accessing the contacts in your phone.

Checking whether permission is already granted , if not requesting for permisssion

First step of requesting permission is to check whether the permission is already granted by the user or not. Because we need to get one permission only once . So if it is already granted we don’t need to request again. The following code can be used to verify whether the permission is already granted and how to request for permission if not already granted.

//checking wether the permission is already granted
if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED){

// permission is already granted
// here you can directly access contacts

}else{

//persmission is not granted yet
//Asking for permission
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS},REQUEST_CODE);

}

Handling the result based on users decision

The following code will contain the callback of request permission popup, which will allow you to navigate the user based on the decision he made on the permission request. Some application won’t allow the user to use application if the required permission is not granted. You can manage that also by placing the code to quit application in the else loop of the following code

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

if(requestCode == REQUEST_CODE){

//checking if the permission is granted
if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

//permission is granted,do your operation


}else{

// permission not granted
//Display your message to let the user know that he requires permission to access the app
Toast.makeText(this,"You denied the permission",Toast.LENGTH_LONG).show();

}
}
}

Note: initialize the REQUEST_CODE in your class with an integer number

private int REQUEST_CODE = 1;

How to request multiple permissions

The above code was for requesting and handling a single permission. Now we will learn how to request for multiple permission at the same time. For that first we need to have an array of permissions which is required like follows.

 String[] permissions = new String[]{
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.GET_ACCOUNTS};

Now we need to check any of the permission is already granted or not. For checking that implement the following function and call that function where you required to have the permission. The function will return true if all the permissions in the list is already granted . If any of the permission is missing it will request the missing permission.

private boolean checkPermissions() {
int result;
List<String> listPermissionsNeeded = new ArrayList<>();
for (String p : permissions) {
result = ContextCompat.checkSelfPermission(MainActivity.this, p);
if (result != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(p);
}
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), MULTIPLE_PERMISSIONS);
return false;
}
return true;
}

Now we are checking whether the permission is granted and if not we are requesting it. Next we need to have code for handing multiple permission request . For implementing the same you just need the following code in your activity

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MULTIPLE_PERMISSIONS: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

// all permissions are granted.
} else {

//permissions missing

}
return;
}
}
}

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *