How to end an incoming call programmatically on Android 8.0 Oreo

后端 未结 5 1985
北海茫月
北海茫月 2020-12-06 05:12

Up to Android 7.1 it was possible to end an incoming call by using the ITelephony.endCall() method and giving your app the permissions

5条回答
  •  遥遥无期
    2020-12-06 06:10

    Changed App Target and Compile level to 28.

    And following permissions.

    
    
    

    Add the following code on onCallStateChanged method of MyPhoneStateListener class.

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

提交回复
热议问题