如何扩展ConnectionService?

时间:2019-03-13 00:28:44

标签: java android android-8.1-oreo android-studio-3.3

我想实现一个简单的应用程序,使我可以过滤传入的电话。我试图使用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);
}

1 个答案:

答案 0 :(得分:1)

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

享受编码。