首页 > 解决方案 > 如何以编程方式挂断奥利奥及以上的电话?

问题描述

我正在构建一个应用程序,它为用户提供了一种在激活服务后阻止所有来电的方法。

在针对较新的 SDK 版本(在我的情况下为 Oreo 及更高版本)方面,显然缺乏解决该问题的方法。我最接近解决方案的是this thread,但该问题没有公认的答案,在我的情况下也没有任何提供的答案。

以下是我也访问过的一些链接:

我的方法是使用前台服务并监听电话状态的变化。

@Override
public void onCreate() {
    super.onCreate();
    TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(new CallStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
    Log.d(TAG, "Called onCreate() method");
}

这是 CallStateListener

private class CallStateListener extends PhoneStateListener {
    @Override
    public void onCallStateChanged(int state, String phoneNumber) {
        if (state == TelephonyManager.CALL_STATE_RINGING) {
            Log.d(TAG, "Phone is ringing");
            TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
            try {
                //Gain access to ITelephony getter
                Class c = Class.forName(telephonyManager.getClass().getName());
                Method method = c.getDeclaredMethod("getITelephony");
                method.setAccessible(true);
                com.android.internal.telephony.ITelephony telephony = (ITelephony) method.invoke(telephonyManager);

                //Hung up the call
                telephony.endCall();
            } catch (Exception e) {
                Log.d(TAG, "Exception occurred: " + e.getMessage());
                e.printStackTrace();
            }
        }
    }
}

我可以确认Phone is ringing收到日志消息

我得到的例外是Exception occurred: MODIFY_PHONE_STATE permission required.

MODIFY_PHONE_STATE第三方应用程序开发人员无法获得权限。我已经看到了修改 linter 的建议,但我仍然无法请求此权限,因此异常仍然存在。

在这些 SDK 版本中是否有其他挂断电话的方法?如果是这样,我很想被任命为正确的方向。谢谢你。

标签: javaandroidpermissions

解决方案


推荐阅读