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

时间:2016-02-27 19:06:54

标签: android 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>

但遗憾的是返回 NULL 帮助我理解。

1 个答案:

答案 0 :(得分:1)

我正在使用这个插件:

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

用法非常简单:

//This method is called whenever the mouse button is held down
private void TableLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
{
    Pick((Control)sender, e.X, e.Y);
}

//This method is called whenever the mouse button is released
private void TableLayoutPanel1_MouseUp(object sender, MouseEventArgs e)
{
    Drop((Control)sender);
}

//This method is called when you move the mouse around
private void TableLayoutPanel1_MouseMove(object sender, MouseEventArgs e)
{
    if (dragging)
    {
        Left = e.X + Left - dragAt.X;
        Top = e.Y + Top - dragAt.Y;
    }
    else dragAt = new Point(e.X, e.Y);
}