问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/32271400/what-is-the-use-case-for-contextcompat-checkselfpermission