【问题标题】:ConnectionService implementation for OutgoingCallOutgoingCall 的 ConnectionService 实现
【发布时间】:2017-11-30 10:56:39
【问题描述】:

我正在尝试实现 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);
}

这是我的连接服务:

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 相关的其他线程中提到的所有选项,但无法获得所需的结果,有人可以帮助我吗?

【问题讨论】:

  • 你能实现它吗?我只能使用 ConnectionService 进行虚假/虚拟通话,但我需要能够进行实际通话。检查了Android源代码,看起来有很多事情需要做。我知道应该有更好的方法。
  • @artsylar:不,我无法让它工作,我不确定过去两年左右情况是否发生了变化,但当时很明显,Android 不允许“获取我的拨出电话断开的 disconnectCause"
  • @Saurabh7474 我坚持实现 connectionService API。您能否提供一些集成连接服务的示例。

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


【解决方案1】:

一些事情:

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

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

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

【讨论】:

  • TestConnectionService 不是仅用于进行虚拟/假呼叫吗?我也在尝试使用 ConnectionService 来实现我们自己的呼叫应用程序。我希望我的应用能够拨打真正的运营商电话。是否可以使用 ConnectionService 类?
  • 我建议将 TestConnectionService 作为如何实现 API 的示例。 ConnectionService API 不允许您的应用发出真正的运营商呼叫。其目的主要是包装 VoIP 呼叫解决方案的功能,以便呼叫将出现在设备的拨号器/电话应用程序中。如果你想拨打电话,你需要权限 android.permission.CALL_PHONE 然后可以使用 TelecomManager#placeCall 拨打电话。
  • 谢谢@TGunn!如果它不允许您的应用拨打真正的运营商电话,那么是否可以创建自己的通话应用,并在通话期间播放音乐等附加功能。
  • @TGunn 您能否提供一些实现连接服务的示例。
  • 示例代码很有帮助,感谢@TGunn
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多