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

Android Deep Link配置问题:点击URL无法唤起应用如何修复?

修复Android Deep Link无法唤起应用的问题

我来帮你排查下Deep Link无法唤起应用的问题~看了你的代码,核心问题出在Manifest里<data>标签的pathPrefix配置上,下面一步步给你解决:

1. 修正Manifest中的路径匹配规则

你当前写的pathPrefix="/*"是错误的——Android的pathPrefix是用来匹配URL路径的开头部分,/*这种写法会要求路径必须以/加任意字符开头,但实际上我们需要匹配根路径下的所有子路径,只需要把pathPrefix改成"/"即可。

修改后的Manifest代码:

<activity android:name=".activity.PostDetailsActivity" 
          android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation" 
          android:screenOrientation="sensorPortrait" 
          android:theme="@style/DetailsActivityTheme" >
    <!-- DeepLink -->
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <!-- 修复pathPrefix,匹配根路径下所有子路径 -->
        <data android:host="www.example.ir" android:pathPrefix="/" android:scheme="http" />
        <data android:host="example.ir" android:pathPrefix="/" android:scheme="http" />
    </intent-filter>
</activity>

如果想要更灵活地匹配任意路径(包括网站根目录本身),也可以用pathPattern=".*"替代pathPrefix:

<data android:host="www.example.ir" android:pathPattern=".*" android:scheme="http" />
<data android:host="example.ir" android:pathPattern=".*" android:scheme="http" />

2. 验证基础配置的正确性

你的intent-filter已经包含了Deep Link必需的三个核心配置:

  • <action android:name="android.intent.action.VIEW" />:响应VIEW类型的Intent
  • <category android:name="android.intent.category.DEFAULT" />:允许隐式唤起应用
  • <category android:name="android.intent.category.BROWSABLE" />:允许浏览器唤起应用
    这部分是完全正确的,不用修改。

3. 测试时的关键注意事项

  • 必须重新安装应用:修改Manifest后,旧的应用缓存不会生效,一定要卸载后重新安装,或者用Android Studio的Run按钮重新部署。
  • 用adb命令验证功能:如果点击URL还是在浏览器打开,可以用adb命令直接测试Deep Link是否正常:
adb shell am start -W -a android.intent.action.VIEW -d "http://example.ir/2018/06/24/content_title_url"

如果这个命令能成功唤起你的PostDetailsActivity,说明配置已经正确,只是部分浏览器会优先自己打开URL,需要用户手动选择你的应用。

  • Android 12+的自动验证(可选):如果想让浏览器直接唤起应用而不弹出选择器,需要在<intent-filter>中添加android:autoVerify="true",同时在你的网站根目录放置assetlinks.json文件(用来验证域名归属),不过这一步是优化项,先确保基础唤起功能正常。

4. Java代码无需修改

你写的获取Uri的代码是正确的:

Intent intent2 = getIntent(); 
Uri data = intent2.getData(); 
if (data!=null){ 
    Toast.makeText(mActivity, ""+data, Toast.LENGTH_SHORT).show(); 
}

当应用被成功唤起时,这段代码会正常显示对应的URL。

按照上面的步骤修改后,重新安装应用再测试,应该就能通过URL唤起你的应用啦~

内容的提问来源于stack exchange,提问作者Dr.Kong

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:07:37