当有电话拨入,处于响铃状态的时候,我们设置电话监听器去挂断电话(aidl,反射),达到拦截电话的效果,挂断电话号码的方法放置在aidl文件中名称为endCall的方法。在我们的工程中新建com.android.internal.telephony包,将NeighboringCellInfo.aidl放入该包中,再新建android.telephony包,将NeighboringCellInfo.aidl放到该包下,这两个文件我会在下面给出。
电话监听的代码:
private class MyPhoneStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_RINGING:
//挂断电话 aidl文件中
System.out.println("响铃了");
endCall(incomingNumber);
break;
}
}
}
endCall方法:
private void endCall(String phone) {
String mode = numberDao.query(phone);
if(mode.equals("2")||mode.equals("3")){
// ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
//ServiceManager此类android对开发者隐藏,所以不能去直接调用其方法,需要反射调用
try {
//1,获取ServiceManager字节码文件
Class<?> clazz = Class.forName("android.os.ServiceManager");
//2,获取方法
Method method = clazz.getMethod("getService", String.class);
//3,反射调用此方法
IBinder iBinder = (IBinder) method.invoke(null, Context.TELEPHONY_SERVICE);
//4,调用获取aidl文件对象方法
ITelephony iTelephony = ITelephony.Stub.asInterface(iBinder);
//5,调用在aidl中隐藏的endCall方法
iTelephony.endCall();
} catch (Exception e) {
e.printStackTrace();
}
}
}
添加权限:<uses-permission android:name="android.permission.CALL_PHONE"/>