OutgoingCall的ConnectionService实现

时间:2017-06-27 11:41:46

标签: android android-6.0-marshmallow phone-call

我正在尝试实现ConnectionService,PhoneAccount和PhoneAccountHandle以获取disconnectCause以断开我的拨出电话(例如,用户拒绝了呼叫,无法访问等) 到目前为止,我已经能够使用customPhoneAccount发起呼叫但是电话呼叫从未通过,因此我无法得到响应。

这是我现在写的代码:
register()是从我的活动中调用以注册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相关的其他线程上提到的所有选项,但无法获得所需的结果,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

一些事情:

  1. 您不应使用具有特殊用途的PhoneAccount.CAPABILITY_CONNECTION_MANAGER,该用途不适用于您的使用案例且需要特殊权限。

  2. 您的onCreateOutgoingConnection方法不应该调用超级版本;该方法是基类ConnectionService类中的空白方法。 Telecom会调用您的覆盖onCreateOutgoingConnection,以便您可以提供新的Connection实例。

  3. 看一下Android开源TestConnectionService课程;它实现了一个简单的ConnectionServicehttps://android.googlesource.com/platform/packages/services/Telecomm/+/master/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java