ViduraPrasangana
ViduraPrasangana

Reputation: 897

"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

Upvotes: 0

Views: 2006

Answers (3)

surya
surya

Reputation: 11

Use TelecomManger to end call programatically for Android 9 and above using ANSWER_PHONE_CALL permission.

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
    TelecomManager tm = (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
    if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ANSWER_PHONE_CALLS) == PackageManager.PERMISSION_GRANTED) {
        success = tm.endCall();
        Log.d("call state", "call end");
    }
}

Upvotes: 1

cantona_7
cantona_7

Reputation: 1177

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.

Upvotes: 1

Kishan Thakkar
Kishan Thakkar

Reputation: 459

As cantona_7 mentioned MODIFY_PHONE_STATE is system only permission and you cannot access it without root access and there are no workaround to kill a call from pie onward .

As stated by official site:

MODIFY_PHONE_STATE Added in API level 1 public static final String MODIFY_PHONE_STATE

Allows modification of the telephony state - power on, mmi, etc. Does not include placing calls.

Not for use by third-party applications.

Upvotes: 0

Related Questions