1. 简介
DeepLink(深层链接)是一种允许用户通过点击链接直接跳转到应用内特定内容的技术。在移动应用开发中,DeepLink 提供了一种便捷的方式,让用户可以从网页、通知、邮件或其他应用直接进入应用的特定页面,提升了用户体验。
1.1 DeepLink 的工作原理
当用户点击一个 DeepLink 时,Android 系统会按照以下顺序尝试处理该链接:
- 打开用户首选的能够处理该 URI 的应用(如果已设定)
- 打开唯一能够处理该 URI 的应用
- 弹出选择框让用户选择使用哪个应用打开
DeepLink 利用 Android 的 Intent 系统实现路由功能。要使应用能够响应特定的 DeepLink,需要在应用的清单文件中声明相应的 Intent 过滤器。
1.2 DeepLink 的类型
Android 支持三种类型的深层链接:
- 自定义深层链接:使用自定义 URI 架构(例如 example://products/123)
- 网页链接:使用标准的 http 和 https 架构
- 应用链接:经过验证的网页链接,可直接跳转到应用而无需用户选择
2. 基本实现方法
2.1 配置 Intent 过滤器
要在应用中实现 DeepLink,首先需要在 AndroidManifest.xml 文件中为相应的 Activity 添加 Intent 过滤器:
<activity
android:name="com.example.android.GizmosActivity"
android:label="@string/title_gizmos" >
<intent-filter android:label="@string/filter_view_http_gizmos">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- 接受以"http://www.example.com/gizmos"开头的 URI -->
<data android:scheme="http"
android:host="www.example.com"
android:pathPrefix="/gizmos" />
</intent-filter>
<intent-filter android:label="@string/filter_view_example_gizmos">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- 接受以"example://gizmos"开头的 URI -->
<data android:scheme="example"
android:host="gizmos" />
</intent-filter>
</activity>
需要注意的关键点:
-
<action>必须设置为android.intent.action.VIEW -
<category>必须包含BROWSABLE和DEFAULT -
<data>标签定义了 URI 的模式,至少需要指定scheme
2.2 获取和处理传入的数据
在 Activity 中,可以通过以下方式获取 DeepLink 传递的数据:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
// 根据获取到的 URI 数据进行相应处理
if (Intent.ACTION_VIEW.equals(action) && data != null) {
// 处理 DeepLink 数据
handleDeepLink(data);
}
}
3. DeepLinkDispatch 框架
DeepLinkDispatch 是 Airbnb 开发的一个基于注解的库,简化了 DeepLink 的处理过程。
3.1 基本使用
使用 DeepLinkDispatch,可以通过注解方式定义 DeepLink:
@DeepLink("foo://example.com/deepLink/{id}")
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (intent.getBooleanExtra(DeepLink.IS_DEEP_LINK, false)) {
Bundle parameters = intent.getExtras();
String idString = parameters.getString("id");
// 使用 idString 进行相应处理
}
}
}
3.2 多个 DeepLink 处理
一个 Activity 可以处理多个 DeepLink:
@DeepLink({"foo://example.com/deepLink/{id}", "foo://example.com/anotherDeepLink"})
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (intent.getBooleanExtra(DeepLink.IS_DEEP_LINK, false)) {
Bundle parameters = intent.getExtras();
String idString = parameters.getString("id");
// 使用 idString 进行相应处理
}
}
}
3.3 方法级别的注解
还可以在方法上使用注解来创建 Intent:
@DeepLink("foo://example.com/methodDeepLink/{param1}")
public static Intent intentForDeepLinkMethod(Context context, Bundle extras) {
Uri.Builder uri = Uri.parse(extras.getString(DeepLink.URI)).buildUpon();
return new Intent(context, MainActivity.class)
.setData(uri.appendQueryParameter("bar", "baz").build())
.setAction(ACTION_DEEP_LINK_METHOD);
}
4. 最佳实践
4.1 用户体验优化
- DeepLink 应该直接将用户带到相关内容,避免出现提示、中间页面或登录页面
- 确保用户即使从未打开过应用也能看到内容
- 遵循 Android 导航设计指南,确保返回导航符合用户预期
4.2 测试 DeepLink
可以使用 adb 命令测试 DeepLink:
$ adb shell am start
-W -a android.intent.action.VIEW
-d "example://gizmos" com.example.android
5. 总结
DeepLink 是提升应用用户体验的重要技术,它允许用户直接跳转到应用的特定内容。通过合理配置 Intent 过滤器和使用像 DeepLinkDispatch 这样的框架,我们可以轻松实现强大的 DeepLink 功能。在实现过程中,需要注意用户体验的优化和充分的测试,确保 DeepLink 的稳定性和易用性。










网友评论