API 33下从网页打开Android应用并传参的问题排查
Android API 33下网页调起应用并传参的非DeepLink/自定义Scheme实现
问题场景
需要从网页打开Android应用并传递两个参数,因不推荐使用自定义Scheme且无需DeepLink,选择Intent方案,但调试时Chrome仅打开空白页,无法正常启动应用。
修正后的AndroidManifest.xml配置
移除原配置中依赖自定义Scheme的<data>节点,仅通过action和类别匹配Intent,同时确保API33要求的exported属性:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.MyApp"> <application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"> <activity android:name="com.MyApp.activity.MainActivity" android:exported="true" android:label="MyApp"> <intent-filter> <action android:name="com.MyApp.LAUNCH"></action> <category android:name="android.intent.category.DEFAULT"></category> <category android:name="android.intent.category.BROWSABLE"></category> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> </manifest>
修正后的JavaScript代码
优化Intent URI格式,改用更稳定的调用方式,避免Chrome打开空白页问题,同时对参数进行编码:
if (navigator.userAgent.match(/Android/i)) { // 编码参数防止特殊字符解析失败 const encodedP = encodeURIComponent(p); const encodedC = encodeURIComponent(c); // 移除多余的scheme/host配置,仅保留必要的action和package const intentUri = `intent://#Intent;action=com.MyApp.LAUNCH;package=com.MyApp;S.p=${encodedP};S.c=${encodedC};end`; // 优先使用location.href触发Intent location.href = intentUri; // 添加iframe作为 fallback,兼容部分浏览器 const iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.src = intentUri; document.body.appendChild(iframe); setTimeout(() => document.body.removeChild(iframe), 500); }
关键注意事项
- API33权限要求:必须将目标Activity的
android:exported设为true,否则外部应用(包括浏览器)无法调用 - 参数编码:传递参数时必须用
encodeURIComponent处理,避免空格、特殊字符导致Intent解析异常 - 调用方式:避免使用
window.open,改用location.href+iframe组合,Chrome对这种方式的支持更稳定,不会出现空白页 - Intent匹配规则:确保JavaScript中指定的
action与Manifest里的action完全一致(大小写敏感) - 调试环境:必须在真机上测试,部分Android模拟器存在Intent调用的兼容性问题
内容的提问来源于stack exchange,提问作者FrenchCalculator
相关产品推荐
相关产品推荐

