You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Cordova应用出现在拨号列表,替代默认拨号器处理电话号码

让Cordova应用处理系统电话号码点击事件

要实现点击其他应用中的电话号码时,系统弹出选项让用户选择你的Cordova应用,需分别配置Android和iOS平台的协议处理规则,并对接收到的号码数据做自定义处理。

Android 平台配置

  1. 通过config.xml配置Intent Filter
    在项目的config.xml中添加如下配置,为主Activity注册处理tel:协议的规则,让系统识别你的应用可处理电话号码链接:

    <platform name="android">
        <edit-config file="AndroidManifest.xml" target="/manifest/application/activity[@android:name='MainActivity']" mode="merge">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="tel" />
            </intent-filter>
        </edit-config>
    </platform>
    
  2. 获取并处理电话号码
    安装cordova-plugin-intent插件来读取Intent传递的号码:

    cordova plugin add cordova-plugin-intent
    

    在deviceready事件中监听Intent数据:

    document.addEventListener('deviceready', function() {
        window.plugins.intent.getIntent(function(intent) {
            if (intent?.data) {
                // 提取纯号码(去除tel:前缀)
                const phoneNumber = intent.data.replace('tel:', '');
                // 这里编写你的自定义逻辑,比如显示号码、自定义拨号界面等
                console.log('接收到的电话号码:', phoneNumber);
            }
        }, function(err) {
            console.error('获取Intent数据失败:', err);
        });
    }, false);
    

iOS 平台配置

  1. 通过config.xml配置URL Scheme
    在config.xml中添加配置,让应用支持tel:协议的处理:

    <platform name="ios">
        <config-file parent="CFBundleURLTypes" target="*-Info.plist">
            <array>
                <dict>
                    <key>CFBundleURLSchemes</key>
                    <array>
                        <string>tel</string>
                    </array>
                </dict>
            </array>
        </config-file>
        <config-file parent="LSApplicationQueriesSchemes" target="*-Info.plist">
            <array>
                <string>tel</string>
            </array>
        </config-file>
    </platform>
    
  2. 处理接收到的电话号码
    直接使用Cordova原生支持的handleOpenURL事件(需定义在全局作用域):

    function handleOpenURL(url) {
        setTimeout(function() {
            // 提取纯号码
            const phoneNumber = url.replace('tel:', '');
            // 自定义逻辑处理
            console.log('接收到的电话号码:', phoneNumber);
        }, 0);
    }
    

测试说明

  • 配置完成后重新构建应用:cordova build android 或 cordova build ios
  • 测试设备上,点击其他应用中的电话号码链接,即可看到你的应用出现在选择列表中
  • Android平台如果默认拨号器被设为唯一选项,需先清除其默认应用设置

内容的提问来源于stack exchange,提问作者Luís Henrique de Almeida

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 02:37:35