向Android OS通知视频通话,并从其他应用程序接收此类事件

时间:2019-07-10 09:13:59

标签: android phone-call

我正试图为此目的使用TelecomService,但是无法使用我们的字符串用户ID拨打和接听电话。我不想显示系统通话UI,不想与电话通讯录交互,仅想通知系统有关视频通话已开始/已停止的信息,并希望接收来自其他应用程序的通话事件。 首先,我在电信服务中创建用户帐户(根据prankstern1对上一个主题:https://stackoverflow.com/a/55917774/1778879的回答):

fun registerAccount(applicationContext: Context, label: String) {
        val telecomService = applicationContext.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
        val accountHandle = getSavedAccount(applicationContext)
        var phoneAccount = telecomService.getPhoneAccount(accountHandle)
        if(phoneAccount == null) {
            val builder = PhoneAccount.builder(accountHandle, label)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                builder.setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED)
            }
            phoneAccount = builder.build()
            telecomService.registerPhoneAccount(phoneAccount)
        }
    }

    fun getSavedAccount(applicationContext: Context) : PhoneAccountHandle {
        val phoneAccountLabel = BuildConfig.APPLICATION_ID
        val componentName = ComponentName(applicationContext, TalkoutTelecomService::class.java)
        return PhoneAccountHandle(componentName, phoneAccountLabel)
    }

ConnectionService实现(根据本指南:https://developer.android.com/guide/topics/connectivity/telecom/selfManaged):

override fun onCreateOutgoingConnection(connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest?): Connection {
    val telecomService = applicationContext.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
    val phoneAccount = telecomService.getPhoneAccount(connectionManagerPhoneAccount)
    val connection = TalkoutConnection()
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        connection.connectionProperties = PROPERTY_SELF_MANAGED
    }
    connection.setCallerDisplayName(phoneAccount.label.toString(), PRESENTATION_ALLOWED)
    if(request != null) {
        connection.setVideoState(request.videoState)
    }
    connection.setIncominCallExtras(request?.extras)
    return connection
}

override fun onCreateOutgoingConnectionFailed(connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest?) {
    //TODO: implement
}

override fun onCreateIncomingConnection(connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest?): Connection {
    val connection = TalkoutConnection()
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        connection.connectionProperties = PROPERTY_SELF_MANAGED
    }
    if(request != null) {
        val telecomService = applicationContext.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
        val phoneAccount = telecomService.getPhoneAccount(request.accountHandle)
        connection.setCallerDisplayName(phoneAccount.label.toString(), PRESENTATION_ALLOWED)
    }
    if(request != null) {
        connection.setVideoState(request.videoState)
    }
    return connection
}

override fun onCreateIncomingConnectionFailed(connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest?) {
    //TODO: implement
}

放置去电:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    val telecomService = context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
                    try {
                        val uri = Uri.parse(String.format("myapp://%s", teacherInfo.name))
                        val accountHandle = TalkoutTelecomService.getSavedAccount(context)
                        val callBundle = Bundle()
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            callBundle.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, accountHandle)
                            try {
                                telecomService.placeCall(uri, callBundle)
                            } catch (e: Throwable) {
                                e.printStackTrace()
                            }
                        }
                    } catch (e: Throwable) {
                        e.printStackTrace()
                    }
                }
            }

当我尝试发出呼叫时,我看到“错误号码”对话框,仅此而已:ConnectionService中没有onCreateOutgoingConnection调用,Connection子类中没有onShowIncomingCallUi调用。

接听电话:

val telecomService = getSystemService(Context.TELECOM_SERVICE) as TelecomManager
                              val accountHandle = TalkoutTelecomService.getSavedAccount(applicationContext)
                              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                  bundle.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, accountHandle)
                                  telecomService.addNewIncomingCall(accountHandle, bundle)
                              }

或者,也许还有另一种方式来捕获通过通知启动的其他应用程序的呼叫? TelecomManager似乎无法正常工作。可以不使用电话号码但使用我们应用程序的字符串用户标识符来使用它吗?

0 个答案:

没有答案