我正在创建一个离子项目并使用cordova-phone-call-trap获取传入的呼叫状态并能够侦听传入的呼叫状态但不能拒绝来电 . 我在插件上的PhonecallTrap.java中执行一些自定义代码并获取传入的电话号码但是无法拒绝来电 . 下面是更新的代码请检查
public class PhoneCallTrap extends CordovaPlugin {
CallStateListener listener;
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
prepareListener();
listener.setCallbackContext(callbackContext);
return true;
}
private void prepareListener() {
if (listener == null) {
listener = new CallStateListener();
TelephonyManager TelephonyMgr = (TelephonyManager) cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
TelephonyMgr.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
}}class CallStateListener extends PhoneStateListener {
private CallbackContext callbackContext;
private Context context;
TelephonyManager telephonyManager;
private ITelephony telephonyService;
public void setCallbackContext(CallbackContext callbackContext) {
this.callbackContext = callbackContext;
}
public void PhoneCallStateListener(Context context){
this.context = context;
}
public void PhoneCallStateListener(ITelephony telephonyService){
this.telephonyService = telephonyService;
}
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber.toString());
if (callbackContext == null) return;
String msg = "";
try {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
JSONObject Callresponse = new JSONObject();
Callresponse.put("msg","IDLE");
Callresponse.put("incomingNumber",incomingNumber.toString());
msg = Callresponse.toString();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
JSONObject OFFHOOKresponse = new JSONObject();
OFFHOOKresponse.put("msg","OFFHOOK");
OFFHOOKresponse.put("incomingNumber",incomingNumber.toString());
msg = OFFHOOKresponse.toString();
break;
case TelephonyManager.CALL_STATE_RINGING:
JSONObject RINGINGresponse = new JSONObject();
RINGINGresponse.put("msg","RINGING");
RINGINGresponse.put("incomingNumber",incomingNumber.toString());
// Reject the Incoming Calls
rejectCall();
msg = RINGINGresponse.toString();
break;
}
} catch (JSONException e) {
//some exception handler code.
}
PluginResult result = new PluginResult(PluginResult.Status.OK, msg);
result.setKeepCallback(true);
callbackContext.sendPluginResult(result);
}
private void rejectCall(){
try {
Method m1 = tm.getClass().getDeclaredMethod("getITelephony");
m1.setAccessible(true);
Object iTelephony = m1.invoke(tm);
// Method m2 = iTelephony.getClass().getDeclaredMethod("silenceRinger");
Method m3 = iTelephony.getClass().getDeclaredMethod("endCall");
// m2.invoke(iTelephony);
m3.invoke(iTelephony);
} catch (Exception e) {
e.printStackTrace();
}
}}