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

如何将Android应用指定为callback URL以接收条码阅读器回调数据

把Android应用设置为回调URL接收条码扫描结果的实现步骤

1. 配置Manifest的Intent过滤器

首先在你应用需要接收回调的Activity节点下添加intent-filter,声明你自定义的回调URL规则,不需要公网域名,使用私有scheme即可,示例配置如下:

<activity
    android:name=".ScanResultReceiverActivity"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <!-- 自定义回调URL规则,最终回调地址为 myappscan://receive -->
        <data
            android:scheme="myappscan"
            android:host="receive" />
    </intent-filter>
</activity>

你可以根据自己的应用标识修改scheme和host,只要保证是唯一不与其他应用冲突的即可,避免回调被其他应用拦截。

2. 唤起条码阅读器时传入自定义回调地址

点击应用按钮唤起条码阅读器时,直接将你上面配置的自定义URL(示例中为myappscan://receive)作为回调地址参数传给阅读器的唤起链接即可。

3. 在Activity中解析回调数据

条码阅读器完成扫描后,会把扫描结果作为查询参数拼接在你的回调URL后跳转回你的应用,你需要在对应Activity中解析Intent的data字段拿到结果,示例代码如下:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // 处理应用未启动时触发的回调
    parseScanResult(intent)
}

override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)
    // 处理应用在后台运行时触发的回调
    parseScanResult(intent)
}

private fun parseScanResult(intent: Intent?) {
    intent?.data?.let { uri ->
        // 参数名以你使用的条码阅读器的规则为准,常见参数名为code、barcode、result等
        val scanResult = uri.getQueryParameter("code")
        val scanType = uri.getQueryParameter("type")
        // 拿到结果后执行业务逻辑即可
        if (scanResult != null) {
            // 后续处理逻辑
        }
    }
}

注意事项

  • 自定义scheme尽量使用和你应用包名强相关的字符串,比如包名为com.xxx.barcodetool可以设置scheme为comxxxbarcodetool,大幅降低和其他应用冲突的概率
  • 不同条码阅读器的回调参数命名可能存在差异,提前确认你使用的工具对应的参数名即可

内容的提问来源于stack exchange,提问作者Merih Dursun

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 07:54:02