gpt4 book ai didi

java - 如何扩展ConnectionService?

转载 作者:行者123 更新时间:2023-12-02 10:08:58 26 4
gpt4 key购买 nike

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

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

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

我的 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() {

}
}
}

list :

<?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。我两年前做过id,我可能忘记了一些事情。请让我知道您的进展。

享受编码的乐趣。

关于java - 如何扩展ConnectionService?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55132632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com