How can I detect a new incoming call during an active phone call programmatically in Android

383 Views Asked by At

I'm building an Android Dialer App and currently I'm working on a calling feature. The problem that I'm facing right now is when I'm already on a phone call and someone calls me, my app doesn't detect that there is another incoming call. I want to show user new incoming call on the calling screen during an active phone call. How can I detect that and show it to the user? Thanks!

I'm trying this to detect new incoming call but it doesn't work.

class CallActionReceiver : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {
    var state = 0
    when (intent.getStringExtra(TelephonyManager.EXTRA_STATE)) {
        TelephonyManager.EXTRA_STATE_IDLE -> {
            state = TelephonyManager.CALL_STATE_IDLE
        }
        TelephonyManager.EXTRA_STATE_RINGING -> {
            state = TelephonyManager.CALL_STATE_RINGING
        }
        TelephonyManager.EXTRA_STATE_OFFHOOK -> {
            state = TelephonyManager.CALL_STATE_OFFHOOK
        }
    }
    if (lastState != state) {
        when (state) {
            TelephonyManager.CALL_STATE_RINGING -> {
                if (lastState == TelephonyManager.CALL_STATE_OFFHOOK) {
                    context.showToast("New Call In Waiting")
                }
            }
            TelephonyManager.CALL_STATE_IDLE -> {
                if (lastState == TelephonyManager.CALL_STATE_RINGING) {
                    context.showToast("Missed Call")
                }
            }
        }
    }
    lastState = state
    when (intent.action) {
        ACCEPT_CALL -> {
            context.startActivity(CallingActivity.getStartIntent(context))
            CallManager.accept()
        }
        DECLINE_CALL -> {
            CallManager.reject()
        }
    }
}

}

0

There are 0 best solutions below