I am trying to answer automatically incoming calls on my app once they are detected. My app already detects when I have an incoming call, but still does not answer the phone call. I am using the telephonyManager and I have red this tutorial Answer incoming call using android.telecom and InCallService, this one Answer Incoming Call in Android 6.0, and this one Can't answer incoming call in android marshmallow 6.0. If anyone knows how to do it please tell me. I do not mind change my code I just want it done, here my code.
Class where I detect my incoming calls and where I try to auto-answer the incoming calls
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telecom.Log;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.widget.Toast;
import java.io.IOException;
public class InterceptCall extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
try {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)){
Toast.makeText(context, "Ringing!!", Toast.LENGTH_SHORT).show();
TelephonyMethods.AnswerRinginCall(telephonyManager);//method that should answer incoming calls
}
if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK)){
Toast.makeText(context, "Received!!", Toast.LENGTH_SHORT).show();
}
if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)){
Toast.makeText(context, "IDL!!", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
Class where I call to the answerRingingCall() method of the telephonyManager class
public class TelephonyMethods {
public static void AnswerRinginCall(final TelephonyManager manager){
manager.answerRingingCall();
}
}
Method of the telephonyMnager class
/** @hide */
@SystemApi
@RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
public void answerRingingCall() {
try {
ITelephony telephony = getITelephony();
if (telephony != null)
telephony.answerRingingCall();
} catch (RemoteException e) {
Log.e(TAG, "Error calling ITelephony#answerRingingCall", e);
}
}