电话拦截需要得到电话管理器:
tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
然后调用tm的listen方法:
tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
private class MyListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
String mode = dao.findMode(incomingNumber);
if ("1".equals(mode) || "3".equals(mode)) {
//挂断电话
Log.i(TAG, "挂断电话");
endCall();
}
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
}
挂断电话需要调用API可以通过反射调用。
public void endCall() {
// IBinder b = ServiceManager.getService(TELEPHONY_SERVICE); ---->ServiceManager得不到,怎么办???反射
//加载ServiceManager的字节码
try {
Class clazz = CallSmsService.class.getClassLoader().loadClass("android.os.ServiceManager");
Method method = clazz.getDeclaredMethod("getService", String.class);
IBinder iBinder = (IBinder) method.invoke(null, TELEPHONY_SERVICE);
ITelephony.Stub.asInterface(iBinder).endCall();
} catch (Exception e) {
e.printStackTrace();
}
}