87

I am trying to call getCallCapablePhoneAccounts() method of android.telecom.TelecomManager class. Though i have added required user-permission, i am getting Security exception.

Here is the line of code where i am getting exception

List<PhoneAccountHandle> list = getTelecomManager().getCallCapablePhoneAccounts();

user permission added in manifest

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Exception stacktrace Caused by: java.lang.SecurityException: getDefaultOutgoingPhoneAccount: Neither user 10102 nor current process has android.permission.READ_PHONE_STATE. at android.os.Parcel.readException(Parcel.java:1599) at android.os.Parcel.readException(Parcel.java:1552) at com.android.internal.telecom.ITelecomService$Stub$Proxy.getDefaultOutgoingPhoneAccount(ITelecomService.java:615) at android.telecom.TelecomManager.getDefaultOutgoingPhoneAccount(TelecomManager.java:439)

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
Prasad
  • 870
  • 1
  • 6
  • 5
  • 1
    What did you add to your manifest? – Andrew Brooke Sep 23 '15 at 14:31
  • Try a clean and rebuild. If not, deleting the permission and re-adding it supposedly fixes things: http://stackoverflow.com/questions/12778168/access-network-state-permisson-on-android-ics/12778460#12778460 – Chris Stillwell Sep 23 '15 at 14:32
  • this is been added in manifest – Prasad Sep 23 '15 at 14:58
  • Tried clean, rebuild, but issue still exists – Prasad Sep 23 '15 at 14:59
  • I am also getting the same issue, after I upgraded my emulator target android 6. @Prasad were you able to find a solution? – Rusheel Jain Sep 24 '15 at 04:24
  • 3
    It seems this is some issue with Android M https://code.google.com/p/android-developer-preview/issues/detail?id=2938 – Rusheel Jain Sep 24 '15 at 04:29
  • @Rusheel, not yet found the solution. These APIs are added in level 23(android M). It seems to be a bug in the framework – Prasad Sep 24 '15 at 05:40
  • @Prasad You can try to compile your app for api 23 i.e. compile sdk version 23 and target sdk version 23. While on the emulator you can run an older Android like Lollipop (API 21). This should work. I know this is just a workaround to check if your code works fine till a solution for permissions issue is released by android developers – Rusheel Jain Sep 24 '15 at 07:00
  • Finally this is fixed by changing targetSdk level to 4 – Prasad Sep 24 '15 at 08:50

4 Answers4

87

On Android >=6.0, We have to request permission runtime.

Step1: add in AndroidManifest.xml file

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Step2: Request permission.

int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);

if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE_STATE);
} else {
    //TODO
}

Step3: Handle callback when you request permission.

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case REQUEST_READ_PHONE_STATE:
            if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                //TODO
            }
            break;

        default:
            break;
    }
}

Edit: Read official guide here Requesting Permissions at Run Time

Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35
sonnv1368
  • 1,547
  • 12
  • 17
  • 6
    Thank for this. Don't forget, you also need to ensure your activity `implements ActivityCompat.OnRequestPermissionsResultCallback`. – ban-geoengineering Sep 06 '16 at 13:11
  • 3
    the right package is android.Manifest.permission.READ_PHONE_STATE – SalmanShariati Dec 17 '16 at 20:36
  • I got this error on Xiaomi Mi 4i **os version:5.0.2** so what can i do? – Bhavin Patel Mar 28 '17 at 12:07
  • REQUEST_READ_PHONE_STATE here is an app-defined int constant. The callback method gets the result of the request (comment from documentation). – LLAlive Nov 16 '17 at 11:04
  • 1
    I get **Cannot resolve symbol REQUEST_READ_PHONE_STATE** – phrogg Jan 10 '19 at 19:24
  • 4
    Got it, I forgot this `private final int REQUEST_READ_PHONE_STATE=1;` – phrogg Jan 10 '19 at 19:27
  • BTW, by my experience this specific permission READ_PHONE_STATE is acting somewhat strange. Tried on LG G5 API 26. It needs runtime request to be granted, but on the other hand when requested granted automatically without displaying UI, and also does not appear for the user in the app's runtime permissions. – Doron Ben-Ari Dec 23 '22 at 12:57
54

Are you running Android M? If so, this is because it's not enough to declare permissions in the manifest. For some permissions, you have to explicitly ask user in the runtime: http://developer.android.com/training/permissions/requesting.html

LVR
  • 714
  • 5
  • 6
  • 3
    Wrong answer! I am running Android M, but targeting Android L. In such case it IS enough to declare permissions in the manifest. Correct answer to the question is to declare permission `android.Manifest.permission.READ_PHONE_STATE` instead of `android.permission.READ_PHONE_STATE`. – zyamys Apr 26 '17 at 16:56
  • 3
    @zyamys that is not enough: if you target Android L the permission is automatically granted at installation time, but the user can always disable it from system settings. – manfcas May 24 '17 at 11:02
10

I was experiencing this problem on Samsung devices (fine on others). like zyamys suggested in his/her comment, I added the manifest.permission line but in addition to rather than instead of the original line, so:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.Manifest.permission.READ_PHONE_STATE" />

I'm targeting API 22, so don't need to explicitly ask for permissions.

mrrrk
  • 2,186
  • 20
  • 17
0

after I started coding I saw the same issue about my google maps permission. After I read here I understand that same time I asked for permission. I used one bloc to emit location data and should be initialized when open. but the same time I was asking to perm. That means to me while I didn't get the permission I was trying to do something with permission. so I refactor the code first take perm grant from phone and then init all the things with the perm. maybe all kind of "..... neither user have perm.." errors come from this kind of problem.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 30 '23 at 11:52