What is the use case for ContextCompat.checkSelfPermission?

Currently, I have the following runtime resolution check method 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 believe this works well for Marshmallow. However, should I worry about permission in pre-M versions, which I should use instead of ContextCompat.checkSelfPermission () ? I know that permissions in pre-M can be changed using Xposed or similar frameworks, does this mean that ContextCompat.checkSelfPermission() is able to sufficiently detect permission denied due to Xposed, etc.?

+5
source share
1 answer

Depending on the implementation of the permission blocking block (for example, via Xposed), either the application is supplied with fake data or the application process will be canceled.

You wonโ€™t be able to determine if the application is receiving fake data or not, but in this case your application will at least not work.

If the permission is revoked at the process level, then ContextCompat.checkSelfPermission() can detect it even at pre-M and returns PERMISSION_DENIED . Note: if you use the ContextCompat method, you also need to use the ActivityCompat.shouldShowRequestPermissionRationale() and ActivityCompat.requestPermissions() methods or their version of FragmentCompat .

See here for more information. Support library methods for handling permissions .

+2
source

All Articles