Up to Android 7.1 it was possible to end an incoming call by using the ITelephony.endCall()
method and giving your app the permissions
Changed App Target and Compile level to 28.
And following permissions.
Add the following code on onCallStateChanged method of MyPhoneStateListener class.
public void endCall() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
TelecomManager tm = (TelecomManager) mcontext.getSystemService(Context.TELECOM_SERVICE);
if (tm != null) {
boolean success = tm.endCall();
}
// success == true if call was terminated.
} else {
if (mcontext != null) {
TelephonyManager telephony = (TelephonyManager) mcontext
.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.endCall();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}