使用 PhoneGap 在 Android 上自动拨打预定义号码 [英] Call predefined number automatically on Android with PhoneGap

查看:16
本文介绍了使用 PhoneGap 在 Android 上自动拨打预定义号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PhoneGap 和 Jquery 编写一个应用程序,在我的应用程序中我有一个紧急按钮,onclick 它应该自动调用一个预定义的号码.我正在设法打开本机 android 拨号器,但我仍然需要用户再次单击发送按钮进行拨号.

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?

推荐答案

您需要为此功能编写一个插件.您需要做的第一件事是添加:

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

到您的 AndroidManifest.xml.这将允许您跳过拨号器应用程序拨打号码.插件界面需要一点 JavaScript 代码:

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

然后您需要编写一些 Java 代码来进行电话呼叫.您需要创建一个扩展 Plugin 类的新类并编写这样的执行方法:

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

无论你怎么称呼这个类,你都需要在 res/xml/config.xml 文件中添加一行,以便 PluginManager 可以创建它.

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

最后在你的 JavaScript 代码中,你需要创建他们的插件并像这样调用它:

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

应该差不多了.

这篇关于使用 PhoneGap 在 Android 上自动拨打预定义号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆