2

Is it possible to answer call in android programmatically?

I found some where that its not possible but then installed app https://play.google.com/store/apps/details?id=com.a0softus.autoanswer its working fine.

I have searched a lot and tried many things, moreover call rejection is working fine but call answering not working.

I have tried the following code for call answering as shown below:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.KeyEvent;

import java.io.IOException;
import java.lang.reflect.Method;

import app.com.bikemode.MainActivity;
import app.com.bikemode.R;

public class IncomingCallReceiver extends BroadcastReceiver {
    String incomingNumber = "";
    AudioManager audioManager;
    TelephonyManager telephonyManager;
    Context context;
    private MediaPlayer mediaPlayer;

    public void onReceive(Context context, Intent intent) {
        // Get AudioManager
        this.context = context;
        mediaPlayer = new MediaPlayer();
        audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        // Get TelephonyManager
        telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                // Get incoming number
                incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

            }
        }

        if (!incomingNumber.equals("")) {
            // Get an instance of ContentResolver
           /* ContentResolver cr=context.getContentResolver();
            // Fetch the matching number
            Cursor numbers=cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,  new  String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID,ContactsContract.CommonDataKinds.Phone.NUMBER},  ContactsContract.CommonDataKinds.Phone.NUMBER +"=?", new String[]{incomingNumber},  null);
            if(numbers.getCount()<=0){ // The incoming number is not  found in the contacts list*/
            // Turn on the mute
            //audioManager.setStreamMute(AudioManager.STREAM_RING,  true);
            // Reject the call
            //rejectCall();
            // Send the rejected message ton app
            //startApp(context,incomingNumber);
            // }

            //audioManager.setStreamMute(AudioManager.STREAM_RING, true);
            //rejectCall();
            //startApp(context, incomingNumber);
            acceptCall();
        }
    }


    private void startApp(Context context, String number) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("number", "Rejected incoming number:" + number);
        context.startActivity(intent);
    }

    private void rejectCall() {
        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("endCall");
            // Invoke endCall()
            methodEndCall.invoke(telephonyInterface);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    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);*/
            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");
            playAudio(R.raw.a);

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

    }


    private void playAudio(int resid) {
        AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
            mediaPlayer.prepare();
            mediaPlayer.start();
            afd.close();
        } catch (IllegalArgumentException e) {
            Log.w("Rahul Log",e.getMessage());
        } catch (IllegalStateException e) {
            Log.w("Rahul Log", e.getMessage());
        } catch (IOException e) {
            Log.w("Rahul Log", e.getMessage());
        }
    }
}

The function reject call is working fine but accept call is not working.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rahul Vats
  • 277
  • 3
  • 17
  • Possible duplicate of [How can incoming calls be answered programmatically in Android 5.0 (Lollipop)?](https://stackoverflow.com/questions/26924618/how-can-incoming-calls-be-answered-programmatically-in-android-5-0-lollipop) – Nick Cardoso Apr 19 '18 at 08:04

4 Answers4

2

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();
        }

    }
Community
  • 1
  • 1
Daud Arfin
  • 2,499
  • 1
  • 18
  • 37
  • Hi Daaud.. Thanks for the reply.. I have used your code and tested its gving me : Caused by: java.lang.SecurityException: Neither user 10228 nor current process has android.permission.MODIFY_PHONE_STATE. Could you please help me with this... – Rahul Vats Apr 12 '16 at 07:58
  • Sorry, MODIFY_PHONE_STATE is a system-only permission, so you can not access or use this permission in your app. – Daud Arfin Apr 12 '16 at 08:58
  • Is there any other way to do this as some apps on play store doing the same job ? – Rahul Vats Apr 12 '16 at 09:53
  • 1
    Please go through this link http://stackoverflow.com/questions/26924618/how-can-incoming-calls-be-answered-programmatically-in-android-5-0-lollipop – Daud Arfin Apr 13 '16 at 14:22
  • Hi Daud Arfin. Thank you very much its really working. I have tested on Marshmallow...... – Rahul Vats Apr 14 '16 at 10:38
  • This is asking for "MODIFY_PHONE_STATE" permission which will be given only for system apps. Any workarounds to get this working? How other apps are doing on play-store!! – Varun A M Oct 08 '18 at 05:04
0

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();
    }
}
am110787
  • 316
  • 1
  • 2
  • 9
  • Thanks. But after copying code error is coming "Could not resolve symbol ITelephony" – Rahul Vats Apr 12 '16 at 07:41
  • Make sure the package name remain same. `package com.android.internal.telephony; public interface ITelephony { boolean endCall(); void answerRingingCall(); void silenceRinger(); }` – am110787 Apr 12 '16 at 09:04
  • Hi, Thanks after create same package name compile error gone but on call, java.lang.SecurityException: Neither user 10228 nor current process has android.permission.MODIFY_PHONE_STATE exception is coming on function telephonyService.answerRingingCall();. Could you please help me in this. – Rahul Vats Apr 12 '16 at 09:50
  • same problem too – Jemo Mgebrishvili Apr 20 '17 at 08:40
  • @RahulVats - Please add the following permissions ` ` Also, you might have to add the runtime permission for Marshmallow for android.permission.MODIFY_PHONE_STATE – am110787 Jun 22 '17 at 08:14
  • @AspiringDeveloper - There's no change in Telephony Manager in Marshmallow and Nougat so ideally it should work. I haven't test it though. Also, you might have to add the runtime permission for Marshmallow for android.permission.MODIFY_PHONE_STATE – am110787 Jun 22 '17 at 08:16
  • what is type of this variable telephonyService? – SHAHS Jul 21 '17 at 13:33
0
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);
                    }
}
V.Soni
  • 99
  • 1
  • 6
0

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");
            }