7

I'd like to grant android.permission.READ_PRIVILEGED_PHONE_STATE permission to my app, but even though it's a system app (in system-priv folder on a rooted device for tests) it's not granted.

ADB shows request for this permission, but it's not granted.

In the Manifest I have

<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" tools:ignore="ProtectedPermissions" />

When I try to check it in the runtime

if (ActivityCompat.checkSelfPermission(context.getApplicationContext(), Manifest.permission.READ_PRIVILEGED_PHONE_STATE) == PackageManager.PERMISSION_GRANTED)

the AS gives "Cannot resolve symbol 'READ_PRIVILEGED_PHONE_STATE'" and the app won't compile.

Why READ_PRIVILEGED_PHONE_STATE is visible in the Manifest, but not visible in the java code (also missing in Manifest.class) ?

How can I grant this permission?

I tested it on rooted Android Oreo.

Edit: This answers a part of my question: Permission is not a changeable permission type

The permission was granted on Lollipop, but not on Oreo.

But the question remains: Why READ_PRIVILEGED_PHONE_STATE is visible in the Manifest, but not visible in the java code (also missing in Manifest.class) ?

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Janusz Drobiński
  • 101
  • 1
  • 1
  • 4
  • Is this a newly added permission and are you just installing the app with this permission over the existing system app or are you trying to push the app as a system app? – raxerz Jun 22 '19 at 04:34
  • It's a clean install. I'm not installing over and existing app. – Janusz Drobiński Jun 23 '19 at 05:38
  • I don't know why there's no symbol for it, but you can use it's value instead: `ActivityCompat.checkSelfPermission(context.getApplicationContext(), "android.permission.READ_PRIVILEGED_PHONE_STATE")` – Zaffy Nov 15 '19 at 11:19
  • 1
    https://stackoverflow.com/questions/59476817/android-api-29-permission-error-read-privileged-phone-state – Sahil Ekberov Dec 25 '19 at 12:44

1 Answers1

-1

What I think might be happening is that Drobinsky did not call:

 String[] permissions = {"android.permission.READ_PHONE_STATE"};
 int requestCode = 5;
 ActivityCompat.requestPermissions(this,permissions, requestCode);

prior to calling "ActivityCompat.checkSelfPermission(…"

In the activity you can also add a permissions notififcation handler which looks like this:

 @Override
 public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
 switch (requestCode) {
      case 5: //REQUEST_READ_PHONE_STATE:
 if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
      // Do what you want to do with the granted permission

Even though you've got the permission added in the manifest, the user must also grant this permission at runtime. What happens is that a popup will occur asking the user to grant this permission, and if the user grants it, the "checkSelfPermission" method will then work correctly. The popup block is automatic; i.e. no code needs to be added to the app. I think you can add a text message that displays the reason the permission is required. Certain permission requests need to be handled in this manner, whereas some permission requests are granted without user intervention required. I think the Android model is that if a request is deemed "dangerous" it requires the user give their permission to grant the app access to these so called dangerous privileges.

Tom Rutchik
  • 1,183
  • 1
  • 12
  • 15