用于来电通知的Cordova插件

时间:2018-05-08 09:15:19

标签: java android cordova

我需要一个监听来电的插件,这个插件需要与cordova-plugin-tts一起工作,因为当一个电话进来时,语音音频会继续被忽视。 我是Cordova插件开发,java甚至是android studio的新手。 我有勇气尝试这个插件开发,我已经与它争斗了一个多月了。它成了试验和错误的任务。 以下是最后一次java代码尝试和plugin.xml中的intent配置

{
  "customerNumber": "112235-12312",
  "customerName": "A name"
  "orderNumber": "6575-12316",
  "claims": [
    {
        "articleNumber": "24141241",
        "articleName": "Article Name",
        "claimed": 3
    },
        ...
  ],
}

并在plugin.XML

  package com.caller.notifier;

  import org.apache.cordova.CordovaPlugin;
  import org.apache.cordova.*;

  import android.content.BroadcastReceiver;

   import android.content.Context;

    import android.content.IntentFilter;

    import android.content.Intent;

    import android.telephony.TelephonyManager;

  public class speechNotify extends CordovaPlugin {

   private static int lastState = TelephonyManager.CALL_STATE_IDLE;
     private static boolean isIncoming;

     public void initialize(CordovaInterface cordova, CordovaWebView webView) {
    super.initialize(cordova, webView);
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.notify();
   BroadcastReceiver receiver;

       receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
            int state = 0;
            if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                state = TelephonyManager.CALL_STATE_IDLE;
            }
            else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
                state = TelephonyManager.CALL_STATE_OFFHOOK;
            }
            else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){
                state = TelephonyManager.CALL_STATE_RINGING;
            }
            onCallStateChanged(state);
        }

        };
        cordova.getActivity().registerReceiver(receiver, intentFilter);
}


private void onIncomingCallStarted(){
webView.loadUrl("javascript:cordova.fireDocumentEvent('incoming-call');");
}
private void onIncomingCallEnded(){
webView.loadUrl("javascript:cordova.fireDocumentEvent('call-ended');");
}
private void onMissedCall(){
 webView.loadUrl("javascript:cordova.fireDocumentEvent('call-ended');");
}


//Incoming call-  goes from IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when its hung up
private void onCallStateChanged(int state) {
    if(lastState == state){
        //No change, debounce extras
        return;
    }
    switch (state) {
        case TelephonyManager.CALL_STATE_RINGING:
            isIncoming = true;
            onIncomingCallStarted();
            break;
        case TelephonyManager.CALL_STATE_IDLE:
            //Went to idle-  this is the end of a call.  What type depends on previous state(s)
            if(lastState == TelephonyManager.CALL_STATE_RINGING){
                onMissedCall();
            }
            else if(isIncoming){
                onIncomingCallEnded();
            }
            break;
    }
    lastState = state;
    }
    }

这些代码崩溃了应用程序。对于Cordova专家来说,这个插件可能是一个简单的任务。有人应该救我。

0 个答案:

没有答案