"java.lang.SecurityException: MODIFY_PHONE_STATE permission required" 在 android 9 饼中以编程方式终止 phone 调用时

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

我想在 android 9 pie 中以编程方式终止调用。

我使用了这段代码,但它只适用于 Oreo。它不适用于 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;
    }

当 运行 在 android 饼图上时出现此错误。谁能建议我和另一种终止呼叫的方法。

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.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

MODIFY_PHONE_STATE 是一个 system-only 权限,您可以 root 设备并将您的应用程序放在 /system/priv-app 文件夹中。但是会有其他方法可以解决您的问题。你到底想在这里实现什么。

看看here

MODIFY_PHONE_STATE 是一个 system-only 权限,所以不允许应用程序获得它。

这可能与以前版本的平台有所不同,但这没关系,因为它只保护私有 API,所以如果您正在做一些需要它的事情,那么您使用的私有 API 不受支持,将会导致在诸如您的应用在平台的不同版本上崩溃之类的事情上。

您包含的堆栈爬取不完整,因此无法判断您实际在做什么。

cantona_7 所述,MODIFY_PHONE_STATE 是仅系统权限,没有 root 访问权限就无法访问它,并且没有解决方法来终止来自 pie 的调用。

如官网所述:

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.

使用 TelecomManger 使用 ANSWER_PHONE_CALL 权限以编程方式结束 Android 9 及更高版本的通话。

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");
    }
}