科尔多瓦Android Call Reject

时间:2018-11-05 05:00:34

标签: android cordova ionic-framework cordova-plugins visual-studio-cordova

我正在创建一个离子项目,并使用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();
    }

}}

0 个答案:

没有答案