已配置Intent Filter,如何实现Google Play链接跳转至本地APP(无APP则跳转商店/浏览器)
深度链接跳转逻辑实现方案
一、确认Intent Filter配置正确性
你已配置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" /> <!-- 匹配Play商店应用详情URL --> <data android:scheme="https" android:host="play.google.com" android:pathPrefix="/store/apps/details?id=my.app.id" /> <!-- 建议添加自定义Scheme,避免与Play商店URL冲突,提升唤起成功率 --> <data android:scheme="myapp" android:host="open" /> </intent-filter>
划重点:仅用Play商店URL作为深度链接时,部分浏览器会直接强制跳转至商店,搭配自定义Scheme可规避该问题。
二、网页端核心跳转逻辑
在网页中通过JavaScript实现唤起判断,优先尝试打开本地APP,失败则跳转至Play商店或Chrome:
function openTarget() { // 优先用自定义Scheme唤起APP,也可直接使用Play商店URL尝试 const appLink = 'myapp://open'; const storeUrl = 'https://play.google.com/store/apps/details?id=my.app.id'; const startTime = Date.now(); const timeout = 1500; // 超时时间设1.5秒足够覆盖多数设备唤起延迟 // 尝试唤起本地APP window.location.href = appLink; // 超时后检查唤起状态,未成功则跳转至商店 setTimeout(() => { if (Date.now() - startTime < timeout + 100) { window.location.href = storeUrl; } }, timeout); }
- 逻辑说明:若唤起APP成功,页面会切换至APP,定时器内的跳转代码不会生效;若未唤起,超时后自动跳转至商店。
- 注意:必须通过用户交互(如按钮点击)触发该函数,否则易被浏览器拦截。
三、安卓端本地链接拦截(可选)
若在安卓应用内处理该链接,可通过PackageManager直接判断并跳转:
fun handleDeepLink(context: Context, url: String) { val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) val pm = context.packageManager val resolveInfo = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) if (resolveInfo != null && resolveInfo.activityInfo.packageName == "my.app.id") { // 本地APP已安装,直接跳转至对应页面 context.startActivity(intent) } else { // 未安装APP,跳转至Play商店 val storeIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=my.app.id")) context.startActivity(storeIntent) } }
内容的提问来源于stack exchange,提问作者sejn
相关产品推荐
相关产品推荐

