Intent i = new Intent(); i.setAction(Intent.ACTION_CALL); i.setData(Uri.parse("tel:" + address)); i.putExtra("subscription", type);// subscription 是名称,不可改变. type :1为gsm卡|0为电信卡 i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i);
海信手机在挂断电话时,使用反射方法,获取endcall(int) 参数来挂断电话,
try { TelephonyManager mTelephyMgr = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE); Method getITelephonyMethod = mTelephyMgr.getClass().getDeclaredMethod("getITelephonyMSim"); getITelephonyMethod.setAccessible(true); Object objITelephonyMSim = getITelephonyMethod.invoke(mTelephyMgr); Method intEndCall = objITelephonyMSim.getClass().getMethod("endCall", int.class); intEndCall.invoke(objITelephonyMSim, 1);//可挂断主/副卡来电(挂断当前来电) // intEndCall.invoke(objITelephonyMSim, 0);//只能挂断副卡来电 AVLog.d("block", "block the incoming call"); } catch (Exception e) { e.printStackTrace(); }
上面代码中之所以没有将注视的代码去掉是因为,经测试当endcall方法参数为1时,可以挂断主卡和副卡来电,参数为0时,只能挂断副卡来电.那么
intEndCall.invoke(objITelephonyMSim, 1);
以上一句话就是可以挂断两个卡的来电了. 移植项目中测试发现.打进电话测试: 广播接收到"android.intent.action.PHONE_STATE"这个action ,监听到此广播,再判断来电状态
TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE); int callState = tm.getCallState();//获取来电时状态
主卡来电时,上面代码通过,但是在副卡来电获取callState状态时,callState状态是0(PHONE_IDLE =0 电话挂断状态,PHONE_RINGRING = 1是响铃状态).那么继续猜测,副卡来电时,android系统的api方法getCallState()是不能获取副卡的来电状态的,这需要使用反射方法获取主卡和副卡的状态,只要有来电,一定有一个卡的callstate是PHONE_RINGRING状态,代码如下
Method getITelephonyMethod = mTelephyMgr.getClass().getDeclaredMethod("getITelephonyMSim"); getITelephonyMethod.setAccessible(true); Object objITelephonyMSim = getITelephonyMethod.invoke(mTelephyMgr); Method intEndCall = objITelephonyMSim.getClass().getMethod("getCallState", int.class); int state1 = (Integer) intEndCall.invoke(objITelephonyMSim, 0);//主卡状态 int state2 = (Integer)intEndCall.invoke(objITelephonyMSim, 1);//副卡状态 if (state1 == 1 || state2 ==1) { //来电话了,执行拒接逻辑 }