I have just started working with runtime permissions on Android M. I have CAMERA, READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE declared in AndroidManifest.xml
The thing is if user go to Settings and explicitly turn off permissions as below, I cannot check if my app that has Camera or Storage permissions enabled or not
I used this snippet to check if my app has a particular permission or not:
public static boolean hasPermission(Context c, String permission) {
if (Build.VERSION.SDK_INT >= 23) {
return ContextCompat.checkSelfPermission(c, permission) == PackageManager.PERMISSION_GRANTED;
}
return true;
}
Surprisingly, every check with Camera and Storage permissions returns true (granted) even I turned them off in my Settings.
My targetSdkVersion
is 23
Is this expected behavior provided by Google? I looked around the web and still could not find any documentation say anything about handle explicitly-invoked permissions.
Again check for permission in onRestart().
Check out this link for further information: https://github.com/sagarjogadia28/PermissionSample/
You have to change the targetSDKVersion because
you can check this link
add this in your
onCreate
method of activity :-and
override
this method :-You need to do different things.
First, ask for permission what you are trying to perform. Example camera:
Then you need to handle the user response: allow or deny. So Override onRequestPermissionsResult method
EDIT: if you only want to check if the permission is granted or not, you onlu need the first part of the code I've wrote.