Search code examples
androidpermissionstelephony

How to use Android permissions and make them optional for devices that do not have that feature


I have an app that plays audio. I recently added the permission:

android.permission.READ_PHONE_STATE

so I could tell when a call was coming in so I could mute the audio during the call. I also added the permission:

android.permission.CALL_PHONE

So the user could press a icon to call a phone number. These were minor changes and really don't affect how most people use the app. After I published it I now have users who have tablets that don't have phone capability that they can not download the update and new users who have tablets do not see it in the play store anymore.

I read several posts about using this in the manifest instead of the permissions:

<uses-feature android:name="android.hardware.telephony" android:required="false">

But when I try to test the app on the device I get this error:

Caused by: java.lang.SecurityException: Neither user 10022 nor current process has android.permission.READ_PHONE_STATE.

Does anyone have any suggestions on how I can add these minor features to the app without alienating all of the non-phone users?


Solution

  • I read several posts about using this in the manifest instead of the permissions

    You use <uses-feature> in addition to the permissions, not instead of the permissions.

    Quoting the documentation:

    For any of the permissions below, you can disable filtering based on the implied feature by explicitly declaring the implied feature explicitly, in a element, with an android:required="false" attribute.

    So, add back your permissions. Then, use PackageManager and hasSystemFeature() at runtime, to see whether the device has android.hardware.telephony, so you can react as needed.