Matroska
Matroska

Reputation: 6923

How to programmatically answer a call?

I want to answer a phone call. I found the intent android.intent.action.ANSWER but it seems that the only effect that I obtain is an ActivityNotFoundException. Why? Is it a deprecated intent? How can I achieve answer? I have also heard about the "telnet technique". What is that?

Thanks

Upvotes: 7

Views: 22536

Answers (8)

Jana
Jana

Reputation: 189

try {
Runtime.getRuntime().exec("input keyevent"+Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));
} catch (IOException e) {
   //handle error here
}

Upvotes: 0

PravinDodia
PravinDodia

Reputation: 3280

This works from Android 2.2 to 4.0 and now after adding the try catch to the last line it works for 4.1.2 and 4.2 Frankly speaking dont know how it works but it works for me.

    Log.d(tag, "InSecond Method Ans Call");
    // 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));
    sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");

    Intent headSetUnPluggedintent = new Intent(Intent.ACTION_HEADSET_PLUG);
    headSetUnPluggedintent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
    headSetUnPluggedintent.putExtra("state", 0);
    headSetUnPluggedintent.putExtra("name", "Headset");
    try {
        sendOrderedBroadcast(headSetUnPluggedintent, null);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

This is working for me in Android 4.1.2 as well as i have tested on 4.2 This still gives an exception which is handled.

Upvotes: 2

Rookie
Rookie

Reputation: 1

Answer intent should be sent to the InCallScreen.

adb shell am start -n com.android.phone/.InCallScreen -a android.intent.action.ANSWER

Upvotes: 0

stefanogreg
stefanogreg

Reputation: 121

The approach used by simulating a BT handset does not work with all Android Versions and with all Devices as BT handset handling may be different.

The best approach verified in lot of devices and Android versions consists in emulating the pressure and release of the Call button in the Dialer:

Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_CALL));
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_CALL));
context.sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED"); 

You can do that also by sending Shell command "input keyevent 5" via adb or via Runtime.exec but in my case wasn't working for all devices

Upvotes: 1

Seifolahi
Seifolahi

Reputation: 929

you can also send call keyevent to answer calls but the device needs to be rooted

answer calls :

try {
    Thread.sleep(800); 
    Process process = Runtime.getRuntime().exec(new String[]{ "su","-c","input keyevent 5"});
    process.waitFor();

}catch (Exception e) {
    e.printStackTrace();
}

End calls :

try {
    Thread.sleep(800); 
    Process process = Runtime.getRuntime().exec(new String[]{ "su","-c","input keyevent 6"});
    process.waitFor();

}catch (Exception e) {
    e.printStackTrace();
}

Upvotes: 6

midoub
midoub

Reputation: 21

this event dose not work. you should use:

Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_HEADSETHOOK);
i.putExtra(Intent.EXTRA_KEY_EVENT, event );
context.sendOrderedBroadcast(i, null);

that emulate a bhavior of press key.

Upvotes: 2

Gautam Naroji
Gautam Naroji

Reputation: 146

It is possible to answer an Incoming Phone Call. Check this out : here

Upvotes: 3

systempuntoout
systempuntoout

Reputation: 74064

It's not possible, check this thread for further information.

Upvotes: 4

Related Questions