Call a predefined number automatically on Android using PhoneGap

I am writing an application using PhoneGap and JQuery, in my application I have a panic button, onclick it should automatically call a predefined number. I manage to open my own Android dialer, but still I need the user to hit the submit button again to dial.

How can I make a direct call from the application?

+2


source to share


2 answers


You will need to write a plugin for this function. The first thing you need to do is add:

android.permission.CALL_PRIVILEGED

      

to your AndroidManifest.xml. This will allow you to dial the number by skipping the Dialing app. The plugin interface requires a little JavaScript code:

cordova.define("cordova/plugin/emergencydialer", 
  function(require, exports, module) {
    var exec = require("cordova/exec");
    var EmergencyDialer = function () {};

    var EmergencyDialerError = function(code, message) {
        this.code = code || null;
        this.message = message || '';
    };

    EmergencyDialer.CALL_FAILED = 0;

    EmergencyDialer.prototype.call = function(telephoneNumber,success,fail) {
        exec(success,fail,"EmergencyDialer", "call",[telephoneNumber]);
    };

    var emergencyDialer = new EmergencyDialer();
    module.exports = emergencyDialer;
});

      

Then you will need to write Java code to make the phone call. You will need to create a new class that extends the Plugin class and writes the execution method like this:

public PluginResult execute(String action, JSONArray args, String callbackId) {
    PluginResult.Status status = PluginResult.Status.OK;
    String result = "";

    try {
        if (action.equals("call")) {
            String number = "tel:" + args.getString(0);
            Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(number)); 
            this.cordova.getActivity().startActivity(callIntent);
        }
        else {
            status = PluginResult.Status.INVALID_ACTION;
        }
        return new PluginResult(status, result);
    } catch (JSONException e) {
        return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
    }
}

      



Whatever you call this class, you need to add a line to your res / xml / config.xml file so PluginManager can create it.

<plugin name="EmergencyDialer" value="org.apache.cordova.plugins.EmergencyDialer"/>

      

and finally, in your JavaScript code, you will need to create a plugin and call it like this:

function panicButton() {
    var emergencyDialer = cordova.require("cordova/plugin/emergencydialer");
    emergencyDialer.call("18005551212");
}

      

This should do it.

+2


source


This thread is a little older, but seems popular on Google, so I would like to add that there is now a plugin https://github.com/anemitoff/PhoneGap-PhoneDialer that does this. The syntax at the bottom of this page for installing a local plugin seems a little different from what I'm used to, but it did a great job with cordova's recommended syntax for installing local plugins. If you are not familiar with this, you can read about installing plugins from various sources at http://docs.phonegap.com/en/4.0.0/guide_cli_index.md.html and we just did something like

cordova plugin add ../plugins/PhoneDialer

      



and it worked great and was easy to implement!

0


source







All Articles