[在Android中,我可以通过监听电话状态来使用电话API来实现电话拦截器,但是它会显示一个包含电话号码以及拒绝和接受按钮的窗口。由于某些原因,我不需要此窗口。
在我的测试中,我第一次使用设备监视器调用模拟的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();
}
}
我想知道是否有可能完全隐藏窗口并简单地结束通话而没有跟踪,除了通话记录中。
那么您的代码上有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);