77

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
  • 73,189
  • 14
  • 161
  • 193
Prasad
  • 770
  • 1
  • 5
  • 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

3 Answers3

84

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,016
  • 6
  • 21
  • 33
sonnv1368
  • 1,507
  • 11
  • 17
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
  • 2
    This is one possible solution, I don't understand why it is downvoted. – Vedavyas Bhat Dec 30 '15 at 07:40
  • 1
    Yeah you are right. On android M this is the solution +1 :) – Mohammad Jan 03 '16 at 08:58
  • 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
  • 1,995
  • 18
  • 15