Skip to content Skip to sidebar Skip to footer

Call Predefined Number Automatically On Android With Phonegap

I am writing an app using PhoneGap and Jquery, in my app i have a panic button, onclick it should automatically call a predefined number. I'm managing to open the native android di

Solution 1:

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

android.permission.CALL_PRIVILEGED

to your AndroidManifest.xml. This will allow you to dial a number skipping the Dialer app. A small bit of JavaScript code is required for the plugin interface:

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

    varEmergencyDialerError = 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 = newEmergencyDialer();
    module.exports = emergencyDialer;
});

Then you'll need to write some Java code to do the phone call. You'll need to create a new class that extends the Plugin class and write an execute method like this:

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

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

Whatever you call this class you'll need to add a line in the res/xml/config.xml file so the PluginManager can create it.

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

and finally in your JavaScript code you'll need to create they plugin and call it like this:

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

That should about do it.

Solution 2:

This thread is a bit older but seems Google popular so I'd like to add that there is now a plugin at https://github.com/anemitoff/PhoneGap-PhoneDialer that gets this done. The syntax at the bottom of that page for installing a local plugin seems a little different than what I am used to but it worked just fine with the recommended cordova syntax for installing local plugins. If you're not familiar with that you can read up on installing plugins from various sources at http://docs.phonegap.com/en/4.0.0/guide_cli_index.md.html and we simply did something like

cordova plugin add ../plugins/PhoneDialer

and it worked great and was easy to implement!

Post a Comment for "Call Predefined Number Automatically On Android With Phonegap"