如何在android中阻止电话而不显示呼叫?

时间:2019-06-08 13:17:45

标签: android telephony

在Android中,我可以通过监听电话状态使用电话API来实现电话阻止程序,但是它会显示一个包含电话号码以及拒绝和接受按钮的窗口。由于某些原因,我不想要该窗口。

在测试中,我第一次使用Device Monitor调用模拟的Pixel 2 我没有看到该窗口,但此后总是有一个包含电话号码和按钮的窗口。

这是我的代码:

@Override
    public void onReceive(Context context, Intent intent) {

        ITelephony telephonyService;
        try {
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

            if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)) {

                TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                try {
                    Method m = tm.getClass().getDeclaredMethod("getITelephony");

                    m.setAccessible(true);
                    telephonyService = (ITelephony) m.invoke(tm);

                    if ((!isNumberInContactList(number))) {
                        telephonyService.endCall();
                        Toast.makeText(context, "Ending the call from: " + number, Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(context, "Accepting the call from: " + number, Toast.LENGTH_SHORT).show();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }



            }



        } catch (Exception e) {
            e.printStackTrace();
        }
    }

我想知道是否有可能完全隐藏窗口并简单地结束呼叫而不进行跟踪,除了在呼叫日志中。

1 个答案:

答案 0 :(得分:0)

在您的代码中有2条评论: 1-您应该首先检查您的号码是否在开头(isNumberInContactList)内被屏蔽,以避免不必要的步骤(如果不应该被屏蔽) 2行

            telephonyService = (ITelephony) m.invoke(tm);

我不认为它将那样编译,因为ITelephony无法导入 但可以尝试将其更改为:

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