-3

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:

enter image description here

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?

Community
  • 1
  • 1
Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
  • @downvoters Thanks for your feedback on my question! I would really appreciate it if you told me what it is about my question that you don't like, so that I can fix it in my future questions! – Ruchir Baronia Dec 10 '15 at 01:10

1 Answers1

0

Which should return true if the device has phone compatability and false if it doesn't

Um, no.

If you want to know whether the device has telephony capability, use hasSystemFeature(PackageManager.FEATURE_TELEPHONY) on PackageManager.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491