我正在尝试实现ConnectionService,PhoneAccount和PhoneAccountHandle以获取断开我的外发调用的断开原因(例如,用户拒绝了调用,无法接通等)
到目前为止,我已经能够使用customPhoneAccount发起调用,但是该调用从未成功通过,因此无法获得响应。

这是我现在编写的代码:

register()是从我的 Activity 中调用的用于注册phoneAccount的方法:

public void register() {
    TelecomManager manager = (TelecomManager) getSystemService(TELECOM_SERVICE);
    PhoneAccountHandle phoneAccountHandle = new PhoneAccountHandle(
            new ComponentName(getPackageName(),
                    MyConnectionService.class.getName()), "myConnectionServiceId");
    PhoneAccount.Builder builder = PhoneAccount.builder(phoneAccountHandle, "CustomAccount");
    builder.setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_CONNECTION_MANAGER);
    PhoneAccount phoneAccount = builder.build();
    manager.registerPhoneAccount(phoneAccount);
}

发起调用的call()方法:
 public void call() {
    TelecomManager manager = (TelecomManager) getSystemService(TELECOM_SERVICE);
    PhoneAccountHandle phoneAccountHandle = new PhoneAccountHandle(
            new ComponentName(getPackageName(),
                    MyConnectionService.class.getName()), "myConnectionServiceId");
    Bundle test = new Bundle();
    test.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
    manager.placeCall(Uri.parse("tel:" + "1212312312"), test);
}

这是MyConnectionService:
public class MyConnectionService extends ConnectionService {

public static final String TAG = MyConnectionService.class.getName();
@Override
public int onStartCommand(Intent intent,int flags, int startId) {
    Log.d(TAG, "On Start");
    return super.onStartCommand(intent, flags, startId);
}

@Override
public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {
    Connection connection = super.onCreateOutgoingConnection(connectionManagerPhoneAccount, request);
    Log.d(TAG, connection.getDisconnectCause().getReason());
    return connection;
}

@Override
public void onCreateOutgoingConnectionFailed(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {
    if (request != null) {
        Log.d(TAG, request.toString());
    }
    super.onCreateOutgoingConnectionFailed(connectionManagerPhoneAccount, request);
}

我尝试了不同的功能以及与ConnectionService相关的其他线程上提到的所有选项,但无法获得所需的结果,有人可以在这方面帮助我吗?

最佳答案

一些东西:

  • 不应使用PhoneAccount.CAPABILITY_CONNECTION_MANAGER,它具有特殊用途,不适用于您的用例,并且需要特殊权限。
  • 您的onCreateOutgoingConnection方法不应调用 super 版本。该方法在ConnectionService基类中是空白方法。电信调用您的onCreateOutgoingConnection覆盖,以便您可以提供Connection的新实例。

  • 看一下Android Open Source的TestConnectionService类;它实现了一个简单的ConnectionService:
    https://android.googlesource.com/platform/packages/services/Telecomm/+/master/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java

    10-06 07:19