0

I have a method function that is activated when a call is coming and I hit a Button on my screen app.

Below You can see my code:

public void answerCall() throws Exception {

//        answerPhoneHeadsethook(mContext);


        TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        @SuppressWarnings("rawtypes")
        Class c = Class.forName(tm.getClass().getName());
        @SuppressWarnings("unchecked")
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        com.android.internal.telephony.ITelephony telephonyService = (com.android.internal.telephony.ITelephony) m.invoke(tm);
//      telephonyService.answerRingingCall();

        answerPhoneHeadsethookd(mContext);
        answerPhoneAidl(mContext);

    }

    private void answerPhoneAidl(Context context) throws Exception {
        // Set up communication with the telephony service (thanks to Tedd's Droid Tools!)
        TelephonyManager tm = (TelephonyManager) mContext.getSystemService(mContext.TELEPHONY_SERVICE);
        Class c = Class.forName(tm.getClass().getName());
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        ITelephony telephonyService;
        telephonyService = (ITelephony)m.invoke(tm);

        // Silence the ringer and answer the call!
        telephonyService.silenceRinger();
        telephonyService.answerRingingCall();
} 
    private void answerPhoneHeadsethookd(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");
} 

Unfortunately this function is not working.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Lucian Novac
  • 1,255
  • 12
  • 18

1 Answers1

0

Google doesn't want you to do that, and it makes sense, could be abused in so many ways...

If you want to try some "illegal" ways, have a look here: How to programmatically answer/end a call in Android 4.1?

Community
  • 1
  • 1
Wildcopper
  • 373
  • 1
  • 3
  • 11