我想实现一个简单的应用程序,使我可以过滤传入的电话。我试图使用BroadcastReceiver来检测呼叫,然后使用TelecomManager结束呼叫,但是TelecomManager的endCall()需要系统特权。

我现在正在尝试遵循this guide来实现调用应用程序,其中涉及实现ConnectionService类,并使用该类来创建/处理“ Connections”。连接代表一个电话,并且具有setDisconnected()函数,我打算使用该函数来过滤呼叫。

问题在于,似乎我的ConnectionService类未正确绑定到TelecomManager,并且未调用log语句,因此未调用其功能。如何使其正确绑定?

我对ConnectionService的实现:

public class CallHandler extends ConnectionService {

    @Override
    public Connection onCreateOutgoingConnection (PhoneAccountHandle connectionManagerPhoneAccount,
                                                  final ConnectionRequest request) {
        return null;
    }

    @Override
    public void onCreateOutgoingConnectionFailed (PhoneAccountHandle connectionManagerPhoneAccount,
                                                  final ConnectionRequest request) {
    }

    @Override
    public void onCreateIncomingConnectionFailed (PhoneAccountHandle connectionManagerPhoneAccount,
                                                  final ConnectionRequest request) {

    }

    @Override
    public Connection onCreateIncomingConnection (PhoneAccountHandle connectionManagerPhoneAccount,
                                                  final ConnectionRequest request) {
        Log.i("~~~~INFO", "onCreateIncomingConnection was called!!!!");
        return null;
    }


    public class TestConnection extends Connection {
        @Override
        public void onAnswer() {

        }

        @Override
        public void onReject() {

        }
    }
}


表现:

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <service android:name=".CallHandler"
            android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE">
            <intent-filter>
                <action android:name="android.telecom.ConnectionService" />
            </intent-filter>
        </service>

    </application>

</manifest>


main的onCreate中的代码用于注册CallHandler:

private void register() {
    Log.i("Info:", "Register Phone account called");
    TelecomManager manager = (TelecomManager) getSystemService(TELECOM_SERVICE);
    PhoneAccountHandle phoneAccountHandle = new PhoneAccountHandle(
            new ComponentName(getPackageName(),
                    CallHandler.class.getName()), "CallHandlerId");
    PhoneAccount.Builder builder = PhoneAccount.builder(phoneAccountHandle, "CustomAccount");
    builder.setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED);
    PhoneAccount phoneAccount = builder.build();

    manager.registerPhoneAccount(phoneAccount);
}

最佳答案

如果要拒绝某些来电,则不应使用PhoneAccount.CAPABILITY_SELF_MANAGED,因为它只能用于管理自己的电话。您的phoneAccount上需要PhoneAccount.CAPABILITY_CALL_PROVIDER, PhoneAccount.CAPABILITY_CONNECTION_MANAGER并将其设置为系统上的默认phoneAccount。
我两年前做过身份证,我可能会忘记一些事情。请让我知道你的进度。

享受编码。

10-04 19:35