I want to kill a call programatically in android 9 pie.
I used this code but it is only working on Oreo. It`s not work with pie
public static boolean killCall(Context context) {
try {
System.out.println("Kill called");
// Get the boring old TelephonyManager
TelephonyManager telephonyManager =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
// Get the getITelephony() method
Class classTelephony = Class.forName(telephonyManager.getClass().getName());
Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");
// Ignore that the method is supposed to be private
methodGetITelephony.setAccessible(true);
// Invoke getITelephony() to get the ITelephony interface
Object telephonyInterface = methodGetITelephony.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);
System.out.println("Killed");
inCall = false;
} catch (Exception ex) { // Many things can go wrong with reflection calls
ex.printStackTrace();
Log.d(TAG, "PhoneStateReceiver **" + ex.toString());
System.out.println("Error");
return false;
}
return true;
}
It gives this error when run on android pie. Can anyone suggest me and another method to kill a call.
W/System.err: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) W/System.err: at com.hunteralex.autodialer.PhoneStateReceiver.killCall(PhoneStateReceiver.java:122) at com.hunteralex.autodialer.AutoRedialerService$1$1.run(AutoRedialerService.java:97) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.SecurityException: MODIFY_PHONE_STATE permission required. at android.os.Parcel.createException(Parcel.java:1942) at android.os.Parcel.readException(Parcel.java:1910) W/System.err: at android.os.Parcel.readException(Parcel.java:1860) at com.android.internal.telephony.ITelephony$Stub$Proxy.endCall(ITelephony.java:2249) ... 10 more