android.permission.BIND_TELECOM_CONNECTION_SERVICE 权限被拒绝

android.permission.BIND_TELECOM_CONNECTION_SERVICE permission denied

我想为 Voip 拨号器应用实现“android.telephony”库。所以根据这个 https://developer.android.com/reference/android/telecom/ConnectionService 我正在尝试注册 PhoneAccount。我正在做下面的事情,

private void registerPhoneAcc(String calleeURI, String phoneNumber) {
       
        TelecomManager telecomManager = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            telecomManager = (TelecomManager) getContext().getSystemService(Context.TELECOM_SERVICE);
        }
        ComponentName componentName = new ComponentName(getContext().getPackageName(), String.valueOf(MyConnectionService.class));
        PhoneAccountHandle phoneAccountHandle = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            phoneAccountHandle = new PhoneAccountHandle(componentName,"App" );
            PhoneAccount phoneAccount = PhoneAccount.builder(phoneAccountHandle, "Primary").setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER).build();
            telecomManager.registerPhoneAccount(phoneAccount);
        }
       
    }
<service android:name=".app.MyConnectionService"
            android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE">
            <intent-filter>
                <action android:name="android.telecom.ConnectionService" />
            </intent-filter>
        </service>

尽管我已经包含了权限,但我遇到了以下异常

W/System.err: java.lang.SecurityException: PhoneAccount connection service requires BIND_TELECOM_CONNECTION_SERVICE permission.
2021-02-03 13:58:39.259 24630-24630/com.ex.voip W/System.err:     at android.os.Parcel.readException(Parcel.java:2013)
2021-02-03 13:58:39.259 24630-24630/com.ex.voip W/System.err:     at android.os.Parcel.readException(Parcel.java:1959)
2021-02-03 13:58:39.259 24630-24630/com.ex.voip W/System.err:     at com.android.internal.telecom.ITelecomService$Stub$Proxy.registerPhoneAccount(ITelecomService.java:1019)
2021-02-03 13:58:39.259 24630-24630/com.ex.voip W/System.err:     at android.telecom.TelecomManager.registerPhoneAccount(TelecomManager.java:990)
2021-02-03 13:58:39.259 24630-24630/com.ex.voip W/System.err:     at com.app.DialClass.registerPhoneAcc(DialClass.java:828)
2021-02-03 13:58:39.259 24630-24630/com.ex.voip W/System.err:     at com.app.DialClass.handleMessage(DialClass.java:1114)
2021-02-03 13:58:39.259 24630-24630/com.ex.voip W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
2021-02-03 13:58:39.259 24630-24630/com.ex.voip W/System.err:     at android.os.Looper.loop(Looper.java:176)
2021-02-03 13:58:39.260 24630-24630/com.ex.voip W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6651)
2021-02-03 13:58:39.260 24630-24630/com.ex.voip W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2021-02-03 13:58:39.260 24630-24630/com.ex.voip W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)

清单,

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.ex.voip">

    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.group.MICROPHONE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT"/>
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.MANAGE_OWN_CALLS"/>
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="false" />

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true"
        android:xlargeScreens="true" />

    <application
        android:allowBackup="true"
        android:allowClearUserData="true"
        android:fullBackupContent="false"
        android:hardwareAccelerated="false"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:usesCleartextTraffic="true"
        android:resizeableActivity="false"
        android:screenOrientation="portrait"
        android:supportsRtl="true"
        tools:replace="android:label,android:theme,android:icon"
        android:theme="@style/AppThemeNoAction"
        tools:ignore="GoogleAppIndexingWarning">

        <activity
            android:name=".app.LaunchActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:icon="@drawable/logo"
            android:label="@string/app_name"
            android:screenOrientation="sensorPortrait"
            tools:ignore="LockedOrientationActivity"
            android:theme="@style/FullscreenTheme"
            tools:replace="android:icon">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".app.NetworkChangeReceiver"
            android:label="NetworkChangeReceiver" />

        <activity
            android:name=".app.MainActivity"
            android:screenOrientation="sensorPortrait"
            tools:ignore="LockedOrientationActivity"
            android:theme="@style/AppThemeNoActionBar" />
        
        <activity
            android:name=".app.AboutActivity"
            android:screenOrientation="sensorPortrait"
            tools:ignore="LockedOrientationActivity" />
        <activity
            android:name=".app.SpeedActivity"
            android:screenOrientation="sensorPortrait"
            tools:ignore="LockedOrientationActivity" />
        <activity
            android:name=".app.Function"
            android:screenOrientation="sensorPortrait"
            tools:ignore="LockedOrientationActivity"/>
        <activity
            android:name=".app.CallDetails"
            android:screenOrientation="sensorPortrait"
            tools:ignore="LockedOrientationActivity" />

        <activity
            android:name=".app.AccountDetails"
            android:label="@string/title_activity_account_details"
            android:screenOrientation="sensorPortrait"
            tools:ignore="LockedOrientationActivity"
            android:theme="@style/AppThemeNoAction.NoActionBar"/>
        <activity
            android:name=".app.DetailsRecyclerView"
            android:screenOrientation="sensorPortrait"
            tools:ignore="LockedOrientationActivity"/>

        <service android:name=".app.pushnotifications.FCMBroadcastReceiver">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <service
            android:name=".app.StartService"
            android:stopWithTask="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>

        <service
            android:name=".app.CallService"
            android:exported="true"
            android:stopWithTask="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>

        </service>

        <receiver android:name=".app.CallBroadcast"
            android:exported="true"/>

        <service
            android:name=".app.VoipSupportService"
            android:exported="false"
            android:stopWithTask="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <service android:name=".app.NotificationService"
            android:exported="false"
            android:stopWithTask="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <service android:name=".app.MyConnectionService"
            android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE">
            <intent-filter>
                <action android:name="android.telecom.ConnectionService" />
            </intent-filter>
        </service>

    </application>


</manifest>

像这样实现了运行时权限

我的 BIND_TELECOM_CONNECTION_SERVICE 权限请求被拒绝了。

不知道为什么。

提前致谢。

您正在正确使用所有权限:

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.group.MICROPHONE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.MANAGE_OWN_CALLS"/>

但是,您需要为它们添加一项权限:

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

并注意为 Marshmallow 和更高 Android 版本请求此权限。 Request app permissions at Runtime.

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.BIND_TELECOM_CONNECTION_SERVICE)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.BIND_TELECOM_CONNECTION_SERVICE)) {

        // Show an expanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.BIND_TELECOM_CONNECTION_SERVICE},
                MY_PERMISSIONS_REQUEST_BIND_TELECOM_CONNECTION_SERVICE);

        // MY_PERMISSIONS_REQUEST_BIND_TELECOM_CONNECTION_SERVICE is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}