I would like to be able to close the phone call when it rings but from what I read ITELEPHONY is only valid for API level <28.
Is there another way I can block the phone call?
Below is my code and I get
Accessing hidden method Landroid/telephony/TelephonyManager;->getITelephony()Lcom/android/internal/telephony/ITelephony; (greylist-max-p, reflection, denied) 2021-04-28 16:55:41.884 18265-18265/com.example.myapplication W/System.err: java.lang.NoSuchMethodException: android.telephony.TelephonyManager.getITelephony []
I looked for other solutions already posted on stackoverflow but none worked. In the manifest I have added the following permissions: ANSWER_PHONE_CALLS, READ_PHONE_STATE, CALL_PHONE, READ_CALL_LOG.
I forgot to mention that I also have the ITelephony interface with the specific package com.android.internal.telephony;
public class MyPhoneReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephony;
telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
MyPhoneStateListener listener = new MyPhoneStateListener(context);
telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
public static class MyPhoneStateListener extends PhoneStateListener {
Context context;
public MyPhoneStateListener(Context context) {
super();
this.context = context;
}
@Override
public void onCallStateChanged(int state, String callingNumber){
super.onCallStateChanged(state, callingNumber);
if((state == TelephonyManager.CALL_STATE_OFFHOOK) || (state == TelephonyManager.CALL_STATE_RINGING))
{
Log.d("CALLING",callingNumber);
BlockTheCall(callingNumber);
}
}
private void BlockTheCall(String callingNumber)
{
Log.d("CALLING",callingNumber);
try{
TelephonyManager tm;
tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
com.android.internal.telephony.ITelephony telephonyService = (ITelephony) m.invoke(tm);
telephonyService.silenceRinger();
telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}