英文:
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 'call 1' and connecting 'call 2', so 'call 1' is on **HOLD** and 'call 2' is on **ACTIVE** state respectively, now when I tap on merge, it throws an error as
> **"Unable to merge calls"**
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 = "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 cancelCall() {
currentCall?.let {
when (it.state) {
Call.STATE_RINGING -> rejectCall()
else -> disconnectCall()
}
}
}
fun holdCall() {
currentCall?.let {
when (it.state) {
Call.STATE_ACTIVE -> currentCall?.hold()
else -> Log.i(LOG_TAG, "Call not in connected state")
}
}
}
//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 -> currentCall?.unhold()
else -> Log.i(LOG_TAG, "Call not in connected state")
}
}
}
fun isCallOnHold(): Boolean {
currentCall?.let {
return when (it.state) {
Call.STATE_HOLDING -> true
else -> false
}
}
return false
}
fun acceptCall() {
Log.i(LOG_TAG, "acceptCall")
currentCall?.let {
it.answer(it.details.videoState)
}
}
private fun rejectCall() {
Log.i(LOG_TAG, "rejectCall")
currentCall?.reject(false, "")
}
private fun disconnectCall() {
Log.i(LOG_TAG, "disconnectCall")
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>
专注分享java语言的经验与见解,让所有开发者获益!
评论