【问题标题】:How do you extend ConnectionService?如何扩展 ConnectionService?
【发布时间】:2019-03-13 00:28:44
【问题描述】:

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

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

问题是我的 ConnectionService 类似乎没有正确绑定到 TelecomManager 并且它的函数没有被调用,因为日志语句没有发生。如何让它正确绑定?

我的 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);
}

【问题讨论】:

  • 你终于可以成功了吗?我也想知道...

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


【解决方案1】:

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

享受编码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 2020-06-22
    • 2018-08-22
    • 2011-06-09
    • 2010-10-09
    相关资源
    最近更新 更多