如何在Android中以编程方式合并通话?

huangapple 未分类评论46阅读模式
英文:

How to programmatically merge calls in Android?

问题

**通话服务**

我正在使用[InCallService][1]来监听来电[onCallStateChanged][2]回调中更新通话状态我已成功实现了将'通话1'保持并连接'通话2'的功能所以'通话1'处于**保持**状态,'通话2'处于**活动**状态但是当我点击合并时会抛出一个错误

> **无法合并通话**

我正在使用[Conference method][3]来合并通话

为了更好地理解我在下面发布我的代码

```java
class CallService : InCallService() {
    
    override fun onCallAdded(call: Call) {
        super.onCallAdded(call)
        CallManager.updateCall(call)
    }

    private val callCallback = object : Call.Callback() {
        override fun onStateChanged(call: Call, state: Int) {
            CallManager.updateCall(call)
        }
    }

    override fun onCallRemoved(call: Call) {
        super.onCallRemoved(call)
        call.unregisterCallback(callCallback)
        CallManager.updateCall(null)
    }
}

@TargetApi(Build.VERSION_CODES.M)
object CallManager {
    private const val LOG_TAG = "CallManager"
    private val subject = BehaviorSubject.create<GsmCall>()
    private var currentCall: Call? = null
    private var anotherCall: Call? = null

    fun updates(): Observable<GsmCall> = subject

    fun updateCall(call: Call?) {
        if (currentCall != null) {
            call?.let {
                if (it.details.handle.schemeSpecificPart ==
                    currentCall!!.details.handle.schemeSpecificPart
                )
                    currentCall = call
                else anotherCall = call
            }
        } else currentCall = call
        call?.let {
            subject.onNext(it.toGsmCall())
        }
    }

    // ... 其他功能方法 ...

    fun conferenceCall() {
        anotherCall?.conference(currentCall)
    }

    fun swapConferenceCall() {
        currentCall?.swapConference()
    }

    fun mergeConferenceCall() {
        currentCall?.mergeConference()
    }

    // ... 其他功能方法 ...
}

<details>
<summary>英文:</summary>

**In Call Service**

Am using [InCallService][1] to listen for incoming call and in [onCallStateChanged][2] callback am updating call state, I have successfully achieved till holding &#39;call 1&#39; and connecting &#39;call 2&#39;, so &#39;call 1&#39; is on **HOLD** and &#39;call 2&#39; is on **ACTIVE** state respectively, now when I tap on merge, it throws an error as 

&gt; **&quot;Unable to merge calls&quot;**



Am using [Conference method][3] to merge calls

For better understanding, am posting my code below

     class CallService : InCallService() {
        
        override fun onCallAdded(call: Call) {
            super.onCallAdded(call)
            CallManager.updateCall(call)

        }

        private val callCallback = object : Call.Callback() {
            override fun onStateChanged(call: Call, state: Int) {
                CallManager.updateCall(call)
            }
        }

        override fun onCallRemoved(call: Call) {
            super.onCallRemoved(call)
            call.unregisterCallback(callCallback)
            CallManager.updateCall(null)
        }
    }


    @TargetApi(Build.VERSION_CODES.M)
    object CallManager {
        private const val LOG_TAG = &quot;CallManager&quot;
        private val subject = BehaviorSubject.create&lt;GsmCall&gt;()
        private var currentCall: Call? = null
        private var anotherCall: Call? = null
        fun updates(): Observable&lt;GsmCall&gt; = subject


        fun updateCall(call: Call?) {
            if (currentCall != null) {
                call?.let {
                    if (it.details.handle.schemeSpecificPart ==
                        currentCall!!.details.handle.schemeSpecificPart
                    )
                        currentCall = call
                    else anotherCall = call
                }
            } else currentCall = call
            call?.let {
                subject.onNext(it.toGsmCall())
            }
        }


        fun cancelCall() {
            currentCall?.let {
                when (it.state) {
                    Call.STATE_RINGING -&gt; rejectCall()
                    else -&gt; disconnectCall()
                }
            }
        }


        fun holdCall() {
            currentCall?.let {
                when (it.state) {
                    Call.STATE_ACTIVE -&gt; currentCall?.hold()
                    else -&gt; Log.i(LOG_TAG, &quot;Call not in connected state&quot;)
                }
            }
        }

    //Conference call method
        fun conferenceCall() {
            anotherCall?.conference(currentCall)
        }

        fun swapConferenceCall() {
            currentCall?.swapConference()
        }

        fun mergeConferenceCall() {
            currentCall?.mergeConference()
        }

        fun unHoldCall() {
            currentCall?.let {
                when (it.state) {
                    Call.STATE_HOLDING -&gt; currentCall?.unhold()
                    else -&gt; Log.i(LOG_TAG, &quot;Call not in connected state&quot;)
                }
            }
        }

        fun isCallOnHold(): Boolean {
            currentCall?.let {

                return when (it.state) {
                    Call.STATE_HOLDING -&gt; true
                    else -&gt; false
                }
            }
            return false
        }

        fun acceptCall() {
            Log.i(LOG_TAG, &quot;acceptCall&quot;)
            currentCall?.let {
                it.answer(it.details.videoState)
            }
        }

        private fun rejectCall() {
            Log.i(LOG_TAG, &quot;rejectCall&quot;)
            currentCall?.reject(false, &quot;&quot;)
        }

        private fun disconnectCall() {
            Log.i(LOG_TAG, &quot;disconnectCall&quot;)
            currentCall?.disconnect()
        }

    }

   



  


  [1]: https://developer.android.com/reference/android/telecom/InCallService
  [2]: https://developer.android.com/reference/android/telephony/PhoneStateListener#onCallStateChanged(int,%20java.lang.String)
  [3]: https://developer.android.com/reference/android/telecom/Call#conference(android.telecom.Call)

</details>


huangapple
  • 本文由 发表于 2020年4月7日 22:51:57
  • 转载请务必保留本文链接:https://java.coder-hub.com/61082868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定