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

React Native使用Linking自定义URI唤起前台应用报错如何解决?

错误原因

你将自定义URI的<data>配置直接加到了桌面启动对应的intent-filter中,该过滤器仅响应android.intent.action.MAIN类型的启动动作,无法响应Linking发起的android.intent.action.VIEW类型的Intent,因此系统找不到对应处理的Activity。


所需修改

1. 调整AndroidManifest.xml配置

保留原有的启动用intent-filter,新增一个独立的intent-filter用于处理自定义URI,配置如下:

<!-- 原有启动用intent-filter保持不变 -->
<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- 新增自定义URI处理的intent-filter -->
<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="my-custom-uri" />
</intent-filter>

如果需要避免调用URI时新建Activity实例,直接将后台已有的应用拉到前台,可在主Activity标签中添加启动模式配置:

<activity
  android:name=".MainActivity"
  android:launchMode="singleTask"
  <!-- 其他原有配置保持不变 -->
>

2. 优化Linking调用逻辑

调用前先判断URI是否可被处理,避免抛出异常:

Linking.canOpenURL('my-custom-uri://')
  .then(isSupported => {
    if (isSupported) {
      return Linking.openURL('my-custom-uri://');
    }
    console.log('当前设备不支持该自定义URI');
  })
  .catch(error => console.log('调用失败', error));

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 12:54:08