0

So as the subject states I need to be able to answer a phone call programmatically in Android 4.0.3 on an HTC OneX. I have read several places that the MODIFY_PHONE_STATE permission has been revoked by Google so to do this task you need a work around.

I have looked into two avenues so far:

(1) Following Guy's post here and using a BroadcastReceiver

(2) Using the following code to try and hit a key event through a shell command.

final Runtime r = Runtime.getRuntime();
    try {
        Process process = r.exec("input keyevent 5");
        InputStream stream = process.getErrorStream();
        log.v("Process Error Stream: " +stream.toString());
        log.v("Sending shell command to Answer Call");
    } catch (Exception e) {
        log.v("Stack Trace: " + e.getStackTrace().toString());
        e.printStackTrace();
    } 

I use this because keyevent 5 is KeyEvent.CALL according to Google and it works in adb using

adb shell input keyevent 5

My question is, what am I doing wrong? Because logically both of these methods makes sense but neither are working or even generating runtime errors of any kind.

Cheers

Community
  • 1
  • 1
onetwopunch
  • 3,279
  • 2
  • 29
  • 44

3 Answers3

6

After days of research, I found that using both a broadcast receiver route and a runtime.exec() route it is simply not possible to answer a phone call in Android 4.0.3 using the Android API.

For those of you still wondering, I did find some useful information...You CAN answer a call through adb using the command adb shell input keyevent 5 5 is the key code for the call button and in Android it is the KEYEVENT_CALL

Poovizhirajan N
  • 1,263
  • 1
  • 13
  • 29
onetwopunch
  • 3,279
  • 2
  • 29
  • 44
  • I'd imagine something like that would be a security/ux nightmare if they allowed a call to be answered, made, or otherwise tampered with programatically. – Phix Nov 10 '12 at 16:18
  • How exactly do we implement this code? Just replace `input keyevent 5` with `adb shell input keyevent 5`? – Naddy Sep 14 '13 at 23:43
4

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.

PravinDodia
  • 3,271
  • 1
  • 18
  • 22
1

Here are several useful links, check them:

  1. Answer automatically to Incoming Call
  2. How to auto answer call programmatically
  3. Auto answer
Viktor Apoyan
  • 10,655
  • 22
  • 85
  • 147
  • Maybe I was not clear, but Google CHANGED the ability to use the Telephony manager in the newer versions of Android and all of the links you shared either are unsolved or are irrelevant to my question. I need advice on the methods I am using, or help with another way of solving this problem, not the same Google search I have done several times before posting on SO. – onetwopunch Oct 09 '12 at 18:42
  • here I found an article check this one http://grokbase.com/t/gg/android-developers/12547a1nda/cannot-answer-the-incoming-call-during-checking-gmail-account-when-user-forgot-pin-code – Viktor Apoyan Oct 10 '12 at 18:29