gpt4 book ai didi

android - 使用 Cordova 检测 Android 设备上的来电

转载 作者:行者123 更新时间:2023-11-29 01:22:58 25 4
gpt4 key购买 nike

我刚开始学习 Android 编程。我一直在寻找一个很长的来电号码插头。我的搜索尝试没有成功。我决定创建一个插件。我在这里用作示例是来源:

这是我想出的:

package org.apache.cordova.plugin;

import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Date;
import android.content.BroadcastReceiver;
import android.content.Intent;


public class SignalStrength extends CordovaPlugin {

CallStateListener ssListener;
String Number;

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("go")) {
TelephonyManager tm = (TelephonyManager) cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
ssListener = new CallStateListener();
tm.listen(ssListener, PhoneStateListener.LISTEN_CALL_STATE);
callbackContext.success(name);
return true;
}

return false;
}


class CallStateListener extends PhoneStateListener {

@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// called when someone is ringing to this phone
String Number= incomingNumber;
break;
}
}
}

}

list :

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
...
<receiver android:name=".CallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>

但不幸的是返回NULL帮助我理解。

最佳答案

我正在使用这个插件:

https://github.com/renanoliveira/cordova-phone-call-trap

用法很简单:

if (window.PhoneCallTrap) {
PhoneCallTrap.onCall(function(state) {
console.log("CHANGE STATE: " + state);

switch (state) {
case "RINGING":
console.log("Phone is ringing");
break;
case "OFFHOOK":
console.log("Phone is off-hook");
break;

case "IDLE":
console.log("Phone is idle");
break;
}
});
}

关于android - 使用 Cordova 检测 Android 设备上的来电,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35674135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com