Android call answering programmatically

笑着哭i 提交于 2019-12-01 13:41:49
Daud Arfin

I have came across same issue for my app. Put some delay for accepting incoming call(~3000 ms). You also need to invoke acceptCall() method in different thread as well.

Refer How can incoming calls be answered programmatically in Android 5.0 (Lollipop)?

new Thread(new Runnable() {
            @Override
            public void run() {
                acceptCall();
            }
        }).start();

private void acceptCall() {
        try {
            // Get the getITelephony() method
            Class<?> classTelephony = Class.forName(telephonyManager.getClass().getName());
            Method method = classTelephony.getDeclaredMethod("getITelephony");
            // Disable access check
            method.setAccessible(true);
            // Invoke getITelephony() to get the ITelephony interface
            Object telephonyInterface = method.invoke(telephonyManager);
            // Get the endCall method from ITelephony
            Class<?> telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName());
            Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("answerRingingCall");
            // Invoke endCall()
            methodEndCall.invoke(telephonyInterface);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

This works for me.

private void answerCall() {
    Log.d(TAG, "Answering call");
    TelephonyManager telephony = (TelephonyManager)
            context.getSystemService(Context.TELEPHONY_SERVICE);
    try {
        Class c = Class.forName(telephony.getClass().getName());
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        telephonyService = (ITelephony) m.invoke(telephony);
        //telephonyService.silenceRinger();
        telephonyService.answerRingingCall();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
public void answerCall(){  

   try {
                        // logger.debug("execute input keycode headset hook");
                        Runtime.getRuntime().exec("input keyevent " +
                                Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));


                    } catch (IOException e) {

                        Log.e("Call Exception ",e.toString());
                        HelperMethods.showToastS(getBaseContext(),"Call Exception one "+e.toString());
                        // Runtime.exec(String) had an I/O problem, try to fall back
                        //    logger.debug("send keycode headset hook intents");
                        String enforcedPerm = "android.permission.CALL_PRIVILEGED";
                        Intent btnDown = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
                                Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN,
                                        KeyEvent.KEYCODE_HEADSETHOOK));
                        Intent btnUp = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
                                Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP,
                                        KeyEvent.KEYCODE_HEADSETHOOK));

                         sendOrderedBroadcast(btnDown, enforcedPerm);
                        sendOrderedBroadcast(btnUp, enforcedPerm);
                    }
}

this is work for me in android version 9

this code in your answer on click...

if (VERSION.SDK_INT >= 26) {
                IncomingActivity incomingActivity = IncomingActivity.this;
                new Thread(new C1736O(incomingActivity)).start();
            } else if (VERSION.SDK_INT >= 21) {
                IncomingActivity incomingActivity2 = IncomingActivity.this;
                new Thread(new C1735LN(incomingActivity2)).start();
            } else {
                Intent intent = new Intent("android.intent.action.MEDIA_BUTTON");
                intent.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(0, 79));
                IncomingActivity.this.sendOrderedBroadcast(intent, "android.permission.CALL_PRIVILEGED");
                Intent intent2 = new Intent("android.intent.action.MEDIA_BUTTON");
                intent2.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(1, 79));
                IncomingActivity.this.sendOrderedBroadcast(intent2, "android.permission.CALL_PRIVILEGED");
            }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!