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 dialer but still i need the user to click again on send button to dial.

How can I make a direct call from the app?

Answers:

Answer

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");
    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'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:

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'll need to add a line in the res/xml/config.xml file so the PluginManager can create it.

<plugin name="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:

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

That should about do it.

Answer

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!

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.