应用深度链接:App Links与Deep Links实现
你是否还在为用户从网页跳转到App的体验不佳而烦恼?是否遇到过用户点击链接后无法直达指定页面的问题?本文将详细介绍Android平台的两种深度链接技术——App Links和Deep Links,帮助你实现从外部链接直接打开App内指定内容的功能,提升用户体验。读完本文,你将了解两者的区别、实现方法及最佳实践。
深度链接概述
深度链接(Deep Links)是一种能够直接跳转到App内部特定页面的链接技术,而非简单地打开App首页。Android系统支持两种主要的深度链接形式:Deep Links和App Links。
Deep Links
Deep Links是Android系统的标准深度链接功能,利用意图(Intent)系统路由链接到应用。通过自定义URI或标准Web协议,应用可以注册处理特定链接。但Deep Links可能会触发系统的歧义对话框,让用户选择使用哪个应用打开链接。
App Links
App Links是Android 6.0(API级别23)及更高版本引入的增强型深度链接,通过验证应用与网站的关联关系,实现无需歧义对话框直接打开应用的功能。App Links仅适用于指向自有网站的链接,并需要通过资产链接文件验证网站与应用的所有权。
Deep Links实现步骤
1. 在AndroidManifest.xml中配置Intent Filter
在需要处理深度链接的Activity中添加Intent Filter,声明应用可以处理的链接类型。
<activity
android:name=".DeepLinkActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- 自定义URI scheme -->
<data
android:scheme="myapp"
android:host="example.com"
android:pathPrefix="/product" />
<!-- Web URI -->
<data
android:scheme="http"
android:host="www.example.com"
android:pathPrefix="/product" />
<data
android:scheme="https"
android:host="www.example.com"
android:pathPrefix="/product" />
</intent-filter>
</activity>
2. 在Activity中处理深度链接数据
在目标Activity的onCreate()或onNewIntent()方法中获取并解析链接数据。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
handleDeepLink(intent)
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
handleDeepLink(intent)
}
private fun handleDeepLink(intent: Intent) {
if (Intent.ACTION_VIEW == intent.action) {
val data = intent.data
data?.let { uri ->
// 解析URI路径,获取参数
val path = uri.path
val productId = uri.getQueryParameter("id")
// 根据解析结果跳转到相应页面
if (path?.startsWith("/product") == true && productId != null) {
navigateToProductDetail(productId)
}
}
}
}
App Links实现步骤
1. 配置Intent Filter
与Deep Links类似,但需要添加android:autoVerify="true"属性,告诉系统进行App Links验证。
<activity
android:name=".AppLinkActivity"
android:exported="true">
<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="www.example.com"
android:pathPrefix="/product" />
</intent-filter>
</activity>
2. 创建资产链接文件
在网站的.well-known目录下放置assetlinks.json文件,声明网站与应用的关联关系。文件内容示例:
[{
"relation": ["delegate_permission/common.handle_all_urls"],
"target": {
"namespace": "android_app",
"package_name": "com.example.myapp",
"sha256_cert_fingerprints": ["AB:CD:EF:..."]
}
}]
可以使用以下命令获取SHA-256证书指纹:
keytool -list -v -keystore my-release-key.keystore
3. 验证App Links
Android系统会在应用安装时自动验证资产链接文件。也可以通过以下adb命令手动触发验证:
adb shell am start -a android.intent.action.VIEW -d "https://www.example.com/product?id=123" com.example.myapp
深度链接处理最佳实践
1. 使用Navigation组件处理深度链接
Android Jetpack的Navigation组件提供了对深度链接的原生支持,可以在导航图中直接定义深度链接。
<navigation>
<fragment
android:id="@+id/productFragment"
android:name="com.example.ProductFragment">
<deepLink
android:id="@+id/deepLink"
app:uri="https://www.example.com/product/{id}" />
</fragment>
</navigation>
2. 处理链接参数
确保正确解析链接中的参数,并对无效参数进行处理,避免应用崩溃。
3. 添加链接测试
使用Android Studio的App Links Assistant工具测试深度链接,或通过adb命令测试:
adb shell am start -W -a android.intent.action.VIEW -d "https://www.example.com/product?id=123" com.example.myapp
4. 处理应用未安装情况
当用户未安装应用时,应引导用户到应用商店下载。可以通过在服务器端判断链接是否被应用处理,如未处理则重定向到应用商店。
深度链接对比与选择
| 特性 | Deep Links | App Links |
|---|---|---|
| Android版本支持 | 所有版本 | Android 6.0+ |
| 链接类型 | 自定义URI、Web URI | 仅Web URI (http/https) |
| 歧义对话框 | 可能出现 | 无(验证成功后) |
| 网站验证 | 不需要 | 需要资产链接文件 |
| 适用场景 | 应用内部跳转、第三方应用调用 | 自有网站链接跳转 |
总结
深度链接是提升用户体验的重要技术,能够将用户直接引导至App内相关内容。Deep Links适用于简单的深度链接需求,而App Links通过网站验证提供了更流畅的用户体验。根据项目需求选择合适的深度链接方案,并遵循最佳实践,将为用户带来无缝的跳转体验。
更多关于Android开发的资源和库,可以参考项目的README.md文件。贡献指南请参见contributing.md。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考




