Madhukar Hebbar
Madhukar Hebbar

Reputation: 3173

Android check permission

I am developing my project in SDK version 23 where app permissions were newly introduced. In some guidelines they were using below code to read phone state permission is granted or not

if (ContextCompat.checkSelfPermission(serviceContext, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
    //Read Phone state
   }else{
}

But i am directly accessing checkSelfPermission like below

if(serviceContext.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
      //Read Phone state
   }else{
}

It's working fine. My question is what's the difference between above these codes?.which is the correct way to check for permission granted or not?

Upvotes: 2

Views: 20252

Answers (3)

Diako Hasani
Diako Hasani

Reputation: 1494

Add This Code To Gradle

implementation 'com.karumi:dexter:5.0.0'

and Add This Code To Your Code

Dexter.withActivity(getActivity()).withPermission(Manifest.permission.CAMERA).withListener(
                            new PermissionListener() {
                                @Override
                                public void onPermissionGranted(PermissionGrantedResponse response) {
                                  YourCode
                                }

                                @Override
                                public void onPermissionDenied(PermissionDeniedResponse response) {

                                }

                                @Override
                                public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {

                                }
                            }
                    ).check();

Upvotes: 1

Christian
Christian

Reputation: 747

Another solution :

//Requesting permission
private void requestStoragePermission(){

    if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_EXTERNAL_STORAGE)){
        //If the user has denied the permission previously your code will come to this block
        //Here you can explain why you need this permission
        //Explain here why you need this permission
    }

    //And finally ask for the permission
    ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},STORAGE_PERMISSION_CODE);
}

//This method will be called when the user will tap on allow or deny
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    //Checking the request code of our request
    if(requestCode == STORAGE_PERMISSION_CODE){

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

            //Displaying a toast
            Toast.makeText(this,"Permission granted now you can read the storage",Toast.LENGTH_LONG).show();
        }else{
            //Displaying another toast if permission is not granted
            Toast.makeText(this,"Oops you just denied the permission",Toast.LENGTH_LONG).show();
        }
    }
}

Upvotes: 0

MH.
MH.

Reputation: 45493

My question is what's the difference between above these codes?

None, on API 23(+) devices.

Devices running an older version of Android, however, will generate an error through when you try to call context.checkSelfPermission() directly. This method wasn't available until API 23.

ContextCompat provides a backwards compatible way to run checkSelfPermission() on older APIs too. If you look at the implementation, you'll see it accomplishes this by simply delegating the call to checkPermission() with the app's own process parameters. checkPermission() has been available since very first API release and will thus work across the board.

public static int checkSelfPermission(@NonNull Context context, @NonNull String permission) {
    if (permission == null) {
        throw new IllegalArgumentException("permission is null");
    }

    return context.checkPermission(permission, android.os.Process.myPid(), Process.myUid());
}

which is the correct way to check for permission granted or not?

So, to answer this question: if you're only supporting devices running Android 'Marshmallow' 6.0 and newer, then you can use either approach. However, since it's more likely you also want to support some older versions of Android, use ContextCompat.

Upvotes: 8

Related Questions