Search code examples
androidandroid-intentbroadcastreceivertelephonymanager

Android - How to immediately endCall with telephonyService?


In my application, there's a feature that ends outgoing call (and starts other activities) while dialing a certain number (e.g. *123*)

It's currently working, but requires a 200ms delay. Without the delay, the intent cannot be recieved.

The delay causes a consequence of multiple screen flickers:

my activity shows -> switch to call -> end call -> switch back to my activity

public class OutgoingCallListener extends BroadcastReceiver {
    // ...
    public void onReceive(final Context context, Intent intent) {
        // ...
        if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
            // ...
            if(number.equals("*123*")) {
                // ...
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    public void run() {
                        telephonyService.endCall();
                    }
                }, 200);
            }
        }
    }
}

I'v seen other applications with this special-number-dialing feature, the call ends immediately wihout the end-call-beep, and switches to app activity without flickering.

Q1: Does anyone know how to end call without delay? Is there another intent that we can catch before ACTION_NEW_OUTGOING_CALL?

Q2: On a mobile phone with low specs (slow CPU, less memory), would BroadcastReceiver work the same way as on a decent phone?


Solution

  • Got the answer...

    To end an outgoing call immediately, we don't even need to call endCall() from ITelephony, instead, we can simply use setResultData(null);

    It's different from manually ending a call or using endCall, with setResultData(null):

    • No notification icon or message
    • No calling screen
    • No call time toast
    • No call log
    • No end call beep

    It's just like nothing happened (if...without any other extra activities).

    public class OutgoingCallListener extends BroadcastReceiver {
        // ...
        public void onReceive(final Context context, Intent intent) {
            // ...
            if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
                // ...
                if(number.equals("*123*")) {
                    setResultData(null);
                    // start other activities
                }
            }
        }
    }