Search code examples
javaandroidtelephony

android answer incoming call


I have intercepted incoming call and show my activity over standard screen. On this activity I have buttons "answer call" and "reject call", but I can't do it working.

I have found two solutions to answer/reject phone call programaticaly:

  1. With ITelephony.aidl but it works only before api v10. So it is wrong way
  2. With following:

    private void acceptCall(Context context) {
      Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);
      buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(
            KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
      context.sendOrderedBroadcast(buttonDown,
            "android.permission.CALL_PRIVILEGED");
        // froyo and beyond trigger on buttonUp instead of buttonDown
      Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
      buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(
            KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
      context.sendOrderedBroadcast(buttonUp,
            "android.permission.CALL_PRIVILEGED");
    }
    
    
    private void acceptCall(Context context) {
      Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);
      buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(
            KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
      context.sendOrderedBroadcast(buttonDown,
            "android.permission.CALL_PRIVILEGED");
      // froyo and beyond trigger on buttonUp instead of buttonDown
      Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
      buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(
            KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
      context.sendOrderedBroadcast(buttonUp,
            "android.permission.CALL_PRIVILEGED");
    }
    

from here http://www.codeproject.com/Tips/578817/Reject-and-Accept-an-Incoming-Call

However nothing! I click buttons and invoke methods above accordingly, and nothing happens. I tested it on several devices, and result the same.

Hmm, then I was successful with answering on samsung nexus one, but not on HTC with sense. Reject function doesnt work on both.


Solution

  • As result I have found http://androidbridge.blogspot.com/2011/05/how-to-answer-incoming-call-in-android.html

    Here is solution which works on HTCs devices:

    In Android 2.3.3 HTC Sensation this piece of code does not work. Reason is in 2.3.3 I found a HeadsetObserver listening for actual bluetooth plug-in event. So you need to send a Intent pretending there is a headset connected already. To fix this problem you need to send the ACTION_HEADSET_PLUG Intent before calling the above code.

    So I added following code before accept call code:

    Intent headSetUnPluggedintent = new Intent(Intent.ACTION_HEADSET_PLUG);
     headSetUnPluggedintent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
     headSetUnPluggedintent.putExtra("state", 1); // 0 = unplugged  1 = Headset with microphone 2 = Headset without microphone
     headSetUnPluggedintent.putExtra("name", "Headset");
     // TODO: Should we require a permission?
     sendOrderedBroadcast(headSetUnPluggedintent, null);
    

    so final worked code is

        private void acceptCall() {
        setHeadSetConnectEmulated();
        Intent buttonUP = new Intent(Intent.ACTION_MEDIA_BUTTON);
        buttonUP.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
        IncomingCallActivity.this.getApplicationContext().sendOrderedBroadcast(buttonUP, "android.permission.CALL_PRIVILEGED");
    }
    
    private void setHeadSetConnectEmulated(){
        Intent headSetUnPluggedintent = new Intent(Intent.ACTION_HEADSET_PLUG);
        headSetUnPluggedintent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
        headSetUnPluggedintent.putExtra("state", 1); // 0 = unplugged  1 = Headset with microphone 2 = Headset without microphone
        headSetUnPluggedintent.putExtra("name", "Headset");
        // TODO: Should we require a permission?
        sendOrderedBroadcast(headSetUnPluggedintent, null);
    }
    
    private void rejectCall() {
        Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);
        buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
        IncomingCallActivity.this.getApplicationContext().sendOrderedBroadcast(buttonDown, "android.permission.CALL_PRIVILEGED");
    }