首页 > 解决方案 > 用于来电通知的 Cordova 插件

问题描述

我需要一个监听来电的插件,这个插件需要与 cordova-plugin-tts 一起使用,因为当一个呼叫以文本到语音的形式进入时,音频会继续未通知。我是 Cordova 插件开发、java 甚至 android studio 的新手。我有勇气尝试这个插件的开发,我已经和它战斗了一个多月。它变成了一项反复​​试验的任务。下面是最后一次 java 代码尝试和 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;
    }
    }

并在 plugin.XML

        <config-file target="AndroidManifest.xml" parent="/*">
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    </config-file>
        <config-file target="AndroidManifest.xml" parent="/manifest/application">
        <receiver android:name=".CallReceiver" >
        <intent-filter> <action android:name="android.intent.action.PHONE_STATE" /> </intent-filter>
    </receiver>
    </config-file>

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

标签: javaandroidcordova

解决方案


推荐阅读