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

Detecting incoming call on android device with Cordova

我是 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;
            }
        }
    }

}

清单:

<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>

可惜returnsNULL 帮助我理解。

我正在使用这个插件:

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;
        }
    });
}