Kotlin

End a call

The call can be ended by one of two parties:

  • application by the calling the hangup method on the call instance
  • callee by hanging up on the physical device

End call when callee hangups

To end the call (hangup) you need to store the reference to the ongoing call object. Add onGoingCallID property at the top of the MainActivity:

private var onGoingCallID: CallId? = null

You need to store ongoing call reference in the onGoingCallID property and add setOnRTCHangupListener to notify you when the call ends.

In the MainActivity update the body of the onCreate method to include the below listener at the end of the method.

client.setOnCallHangupListener { callId, callQuality, isRemote ->
    onGoingCallID = null
    startCallButton.visibility = View.VISIBLE
    endCallButton.visibility = View.INVISIBLE
}

In the MainActivity update the body of the startCall method. Please make sure to replace PHONE_NUMBER below with the actual phone number you want to call, in the E.164 format (for example, 447700900000):

@SuppressLint("MissingPermission")
fun startCall() {
    client.serverCall(mapOf("to" to "PHONE_NUMBER")) {
        err, outboundCall ->
        when {
            err != null -> {
                connectionStatusTextView.text = err.localizedMessage
            }
            else -> {
                onGoingCallID = outboundCall
                startCallButton.visibility = View.INVISIBLE
                endCallButton.visibility = View.VISIBLE
            }
        }
    }
}

When the call is ended (regardless of who ends the call app or callee) the UI is updated to reflect the current call state (make a call button is shown and END CALL button is hidden).

End call in the application

In the MainActivity fill the body of the hangup method:

private fun hangup() {
    onGoingCallID?.let {
        client.hangup(it) {
            err ->
            when {
                err != null -> {
                    connectionStatusTextView.text = err.localizedMessage
                }
                else -> {
                    onGoingCallID = null
                }
            }
        }
    }
}

Notice that after successful hangup you set the value of the onGoingCallID property back to null.

Build and Run

Press the Ctrl + R keys to build and run the app. Start and end the call to see the UI changes.