java - 用于来电通知的 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 专家来说,这个插件可能是一项简单的任务。有人应该请救我。
解决方案
推荐阅读
- c# - Mongodb:反向文档树作为视图(使用 C# 驱动程序)
- javafx - 在 JavaFX 中单击 Alert 的 OK 按钮打开另一个窗口
- javascript - 如何防止 ASP.NET 页面的回发重置按钮上的类
- ios - 如何从 iOS 的 WebView 应用程序获取 FCM 的注册 ID?
- python - Pandas - 使用 for 循环将更大的数据帧切成更小的数据帧
- pdfbox - 使用 PDFBox 替换图像不会根据图像更改 pdf 的大小
- php - php int比较“小于或等于”返回空还是null?
- firebase - Flutter 状态中的全局变量
- django - 尝试在另一个函数中使用表单输入时,Django TypeError 缺少 1 个必需的位置参数
- java - 程序类型已存在: com.google.android.gms.common.api.zzd ,无法解析