我正尝试在我的应用程序中检测到来电并自动应答。我的应用程序已经可以检测到我有一个来电,但仍然不能自动接听电话。我正在使用telephonyManager,并已阅读了这篇教程Answer incoming call using android.telecom and InCallService、这篇Answer Incoming Call in Android 6.0和这篇Can't answer incoming call in android marshmallow 6.0。如果有人知道如何做到这一点,请告诉我。我不介意更改我的代码,我只想让它做完,这是我的代码。
这是我检测到来电并尝试自动接听的类。
这是我检测到来电并尝试自动接听的类。
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();
}
}
}
我需要调用telephonyManager类的answerRingingCall()方法的类
public class TelephonyMethods {
public static void AnswerRinginCall(final TelephonyManager manager){
manager.answerRingingCall();
}
}
telephonyManager类的方法
/** @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);
}
}