Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use case for ContextCompat.checkSelfPermission?

Currently I have the following method to check for runtime permission in AppCompatActivity for Marshmallow :

if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
    boolean hasPermission = checkSelfPermission(Manifest.permission.XX)
                == PackageManager.PERMISSION_GRANTED;
    if(!hasPermission) {
        if(shouldShowRequestPermissionRationale(Manifest.permission.XX)) {
            // explain reason for permission, try again

        } else {
            // user deny with "don't show again"
        }
    }
}

So far I find it works reasonably well for Marshmallow. However, should I be worrying about permission in pre-M versions that I should use ContextCompat.checkSelfPermission() instead? I know permissions in pre-M can be modified by using Xposed or similar frameworks, does that mean ContextCompat.checkSelfPermission() is able to sufficiently detect permission denials due to Xposed etc too?

like image 234
Neoh Avatar asked Aug 28 '15 12:08

Neoh


People also ask

What is Android checkSelfPermission?

checkSelfPermission(String perm); It returns an integer value of PERMISSION_GRANTED or PERMISSION_DENIED. Note: If a user declines a permission that is critical in the app, then shouldShowRequestPermissionRationale(String permission); is used to describe the user the need for the permission.

What is ActivityCompat?

public interface ActivityCompat. A helper for accessing features in Activity in a backwards compatible fashion. Construct this by using getActivityCompat(Activity) . This helper augments the included methods with data on instant apps.


1 Answers

Depending on the implementation of the permission blocker (e.g. via Xposed) either the app is provided with fake data or the app's process will have the permission revoked.

You won't be able to detect whether the app gets fake data or not, but in that case your app will at least not crash.

If the permission is revoked on process level, then ContextCompat.checkSelfPermission() is able to detect it even on pre-M and returns PERMISSION_DENIED. Note that if you use the ContextCompat method you also have to use the ActivityCompat.shouldShowRequestPermissionRationale() and ActivityCompat.requestPermissions() methods or their FragmentCompat versions.

See here for more details: Support library methods for handling permissions.

like image 130
Floern Avatar answered Oct 05 '22 23:10

Floern