javaandroidandroid-phone-call

"java.lang.SecurityException: MODIFY_PHONE_STATE permission required" when killing a phone call programatically in android 9 pie


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


Solution

  • MODIFY_PHONE_STATE is a system-only permission, you could root device and put your app in /system/priv-app folder. But there will be other way around to solve your problem. What exactly are you trying it to acheive here.

    Have a look here

    MODIFY_PHONE_STATE is a system-only permission, so apps are not allowed to get it.

    This may have changed from previous versions of the platform, but that is okay because it is only protecting private APIs, so if you are doing something that requires it, you are using private APIs that are not supported and will result in things like your app breaking on different builds of the platform.

    The stack crawl you include is not complete, so there is no way to tell what you are actually doing.