2

I am working in Android technology last 1 years. Currently I want develop an application incoming call auto answer in Android 4.0.3 but in this version I am not getting any solution or can't find any api for this (ITelephony.aidl). Please suggest me.

user1551418
  • 21
  • 1
  • 2

3 Answers3

2

Its working code. First find out that its incoming call using Phone state Broadcast Receiver.

    filter.addAction("android.intent.action.PHONE_STATE");
    mContext.registerReceiver(myCallReceiver, filter);

and then in onReceive(Context context, Intent intent) call answerPhoneHeadsethook() function.

private void answerPhoneHeadsethook(Context context) {
    // Simulate a press of the headset button to pick up the call
    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");
}
Shiv
  • 1,269
  • 11
  • 20
0

In order to answer or reject a phone call MODIFY_PHONE_STATE permission is needed. Unfortunately since 2.3 and onwards it is only available for system apps. (more info here)

A workaround to answer the call (originally from here):

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);         
Community
  • 1
  • 1
Guy
  • 5,370
  • 6
  • 25
  • 30
  • 1
    Could you explain a bit of the logic you have here. according to Google, KEYCODE_HEADSETHOOK hangs up the phone. I have tried it several ways but do not get anything even though I can dispatch the event, KEYCODE_CALL through adb (adb shell input keyevent 5) and it works perfectly. I need this in code however. Any ideas? – onetwopunch Oct 04 '12 at 23:47
  • Did the code sample above not work for you? This snippet simulates pressing the headphones button as if headphones are connected. Usually when you click this button it answers a call and/or ends a call if one is already active. Since this is a workaround it will not work 100% of the time on all devices. It depends on internal implementation of the OEMs. – Guy Oct 09 '12 at 07:24
  • please see my question: http://stackoverflow.com/questions/12805796/how-to-programmatically-answer-a-call-in-android-4-0-3 – onetwopunch Oct 09 '12 at 21:26
0

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
  • Security Exception for ACTION_HEADSET_PLUG, permission is not given to non system applications. :( – Viraj Tank Nov 07 '13 at 16:41
  • True, but you can enter the Permission in Manifest, though you get an error it is working. you can download the app from www.virtualmodelz.com in which it is working perfectly. – PravinDodia Nov 20 '13 at 21:08
  • Does not work with Android 4.2.2 :(. Tried adding CALL_PRIVEGED permission. – thomasa88 Jan 14 '14 at 14:45
  • permissions does not work on android 4.0+ devices you need system permissions for the app for giving such permissions but this is working with my and it may be just a bug...not sure but its hard to explain. – PravinDodia Jan 17 '14 at 19:16