Android应用App Linking配置:仅关联网站首页可行吗?
实现Android App Links仅关联网站首页的方案
当然可行!Android的App Links机制完全支持只把网站首页(也就是根路径/)和你的应用关联起来,下面是一步步的实操方法:
1. 配置Digital Asset Links验证文件
首先得在你的网站根目录下放一个assetlinks.json文件,用来让Android系统确认你的应用和网站是合法关联的。这个文件要指定应用包名、签名指纹,并且明确只关联根路径:
[{ "relation": ["delegate_permission/common.handle_all_urls"], "target": { "namespace": "android_app", "package_name": "com.your.app.package", // 替换成你的应用包名 "sha256_cert_fingerprints": ["你的应用SHA256签名指纹"] // 替换成你的签名指纹 }, "include": "/", "exclude": [] }]
重点是include字段设为"/",这样就只会匹配网站的根路径,其他路径不会触发App Link跳转。
2. 在AndroidManifest里配置Intent Filter
打开你的AndroidManifest.xml,在负责处理跳转的Activity(一般是应用的首页Activity)里添加如下Intent Filter:
<activity android:name=".MainActivity"> <!-- 这里保留你原有的Activity配置 --> <intent-filter android:autoVerify="true"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <!-- 只匹配网站根路径 --> <data android:scheme="https" android:host="your-website-domain.com" <!-- 替换成你的网站域名 --> android:path="/" /> </intent-filter> </activity>
这里的核心是android:path="/",它限定了只有访问网站根路径时才会触发这个Intent Filter。而android:autoVerify="true"会让系统自动验证assetlinks.json,完成后用户访问首页就会直接跳转到应用,不会弹出选择器。
3. 处理跳转逻辑(可选)
如果你的应用需要在跳转时做一些额外处理(比如确保打开的是应用首页),可以在对应的Activity的onCreate或者onNewIntent方法里获取Intent数据:
Kotlin示例
override fun onNewIntent(intent: Intent?) { super.onNewIntent(intent) val data = intent?.data if (data != null && data.path == "/") { // 这里写你的处理逻辑,比如导航到应用首页 } }
Java示例
@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); Uri data = intent.getData(); if (data != null && "/".equals(data.getPath())) { // 处理首页跳转逻辑 } }
4. 测试验证
可以用adb命令快速测试跳转是否生效:
adb shell am start -a android.intent.action.VIEW -d "https://your-website-domain.com/"
如果配置正确,应该直接打开你的应用首页,而不是浏览器。另外也可以在手机浏览器里访问你的网站首页,看看是否自动跳转到应用(注意系统需要完成autoVerify验证,一般安装应用后会自动完成,也可以手动等待一会儿或者重启测试)。
内容的提问来源于stack exchange,提问作者Jay V
相关产品推荐
相关产品推荐

