编程语言
首页 > 编程语言> > 如何在Android 8.0 Oreo上以编程方式结束来电

如何在Android 8.0 Oreo上以编程方式结束来电

作者:互联网

Android 7.1之前,可以通过使用ITelephony.endCall()方法结束传入呼叫,并为您的应用提供权限android.permission.CALL_PHONE和android.permission.READ_PHONE_STATE.

在Android 8.0 Oreo(API 26)上执行相同操作时,我收到此错误

12-09 18:11:25.195 16833-16833/li.doerf.leavemealone
E/TelephonyServiceCallHangup: Missing permission MODIFY_PHONE_STATE,
cannot hangup call

由于MODIFY_PHONE_STATE是受保护的权限,我的应用无法获取.有没有办法以编程方式结束Android 8.0上的来电?

解决方法:

将App Target和Compile级别更改为28.

并遵循权限.

<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
<uses-permission android:name="android.permission.READ_CALL_LOG"/>

在MyPhoneStateListener类的onCallStateChanged方法上添加以下代码.

public void endCall() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
            TelecomManager tm = (TelecomManager) mcontext.getSystemService(Context.TELECOM_SERVICE);
            if (tm != null) {
                boolean success = tm.endCall();
            }
            // success == true if call was terminated.
        } else {
            if (mcontext != null) {
                TelephonyManager telephony = (TelephonyManager) mcontext
                        .getSystemService(Context.TELEPHONY_SERVICE);
                try {
                    Class c = Class.forName(telephony.getClass().getName());
                    Method m = c.getDeclaredMethod("getITelephony");
                    m.setAccessible(true);
                    telephonyService = (ITelephony) m.invoke(telephony);
                    // telephonyService.silenceRinger();
                    telephonyService.endCall();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

标签:android-8-0-oreo,telephony,android,android-permissions
来源: https://codeday.me/bug/20190927/1823287.html