I had this exact same problem:
How permission can be checked at runtime without throwing SecurityException?
For the permission to READ_PHONE_STATE
, because I was afraid it would pull an exception for devices which don't have phone compatibility. So, as the answer suggested, I created this method:
private boolean checkReadPhonePermission() {
String permission = "android.permission.READ_PHONE_STATE";
int res = checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}
Which should return true if the device has phone compatability and false if it doesn't. I then wrapped this bit of code which mutes the audio if the user is on the phone, with an if statement based on what the method returns:
For some reason, when I ran this on my emulator and put a toast to see what the method returns, I got false. The emulator device was a phone though, so I expected it to be able to allow that permission...Why is this happening?