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

标签 java android android-phone-call

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

我使用了这段代码,但它仅适用于奥利奥。这不适用于馅饼

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

最佳答案

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

看看here

MODIFY_PHONE_STATE 是仅限系统的权限,因此不允许应用获取它。

这可能与平台的早期版本有所不同,但这没关系,因为它只保护私有(private) API,因此如果您正在做一些需要它的事情,那么您正在使用不受支持的私有(private) API,并且会导致一些问题就像您的应用程序在平台的不同版本上崩溃一样。

您包含的堆栈抓取不完整,因此无法知道您实际在做什么。

关于java - "java.lang.SecurityException: MODIFY_PHONE_STATE permission required"在 android 9 中以编程方式终止电话调用时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57051244/

相关文章:

java - Android:检测何时启用软键盘

java - Android线程时间,utime?

android - 如何将推送通知发送到android中的其他设备?

android - 在进程之间发送大量数据的最佳方法是什么?

android - phonestateintentreceiver.notifyPhoneCallState 有什么用?

java - 如何让Java程序在后台运行?

java - Netty 4中服务器端和客户端高低写水印选项的区别

android - 如何在我的 android 应用程序中以编程方式打开 GPS?

android - 如何发出调用号码的通知?

android - 在应用程序处于自助服务终端模式时调用电话