DeepLink

作者: 放羊娃华振 | 来源:发表于2026-01-05 01:22 被阅读0次

1. 简介

DeepLink(深层链接)是一种允许用户通过点击链接直接跳转到应用内特定内容的技术。在移动应用开发中,DeepLink 提供了一种便捷的方式,让用户可以从网页、通知、邮件或其他应用直接进入应用的特定页面,提升了用户体验。

1.1 DeepLink 的工作原理

当用户点击一个 DeepLink 时,Android 系统会按照以下顺序尝试处理该链接:

  1. 打开用户首选的能够处理该 URI 的应用(如果已设定)
  2. 打开唯一能够处理该 URI 的应用
  3. 弹出选择框让用户选择使用哪个应用打开

DeepLink 利用 Android 的 Intent 系统实现路由功能。要使应用能够响应特定的 DeepLink,需要在应用的清单文件中声明相应的 Intent 过滤器。

1.2 DeepLink 的类型

Android 支持三种类型的深层链接:

  1. 自定义深层链接:使用自定义 URI 架构(例如 example://products/123
  2. 网页链接:使用标准的 http 和 https 架构
  3. 应用链接:经过验证的网页链接,可直接跳转到应用而无需用户选择

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> 必须包含 BROWSABLEDEFAULT
  • <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 用户体验优化

  1. DeepLink 应该直接将用户带到相关内容,避免出现提示、中间页面或登录页面
  2. 确保用户即使从未打开过应用也能看到内容
  3. 遵循 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 的稳定性和易用性。

参考资料

  1. Android 官方文档 - DeepLinking
  2. DeepLinkDispatch GitHub 仓库
  3. 简书 - DeepLink用法及原理解析
    4.https://www.jianshu.com/p/d5db3d2def3b
    5.https://www.5axxw.com/wiki/content/020aoo
    6.https://github.com/airbnb/DeepLinkDispatch

相关文章

  • Web端向App端导量神技Deferred DeepLink的实

    1.什么是DeepLink 2.什么是Deferred DeepLink 3.Deferred DeepLink的...

  • Android DeepLink 技术

    DeepLink 是什么 DeepLink 从字面意思可以理解为「深度链接」,那么 DeepLink 在 Andr...

  • DeepLink

    DeepLink 深度链接 什么是DeepLink DeepLink,又称深度链接、调起链接,是一套链接服务,用户...

  • DeepLink

    一、Intent基础 二、DeepLink基础 实现方式1:(DeepLink为Uri格式) 实现方式2:(Dee...

  • Deeplink使用

    一:deeplink DeepLink: 深度链接技术,主要应用场景是通过Web页面直接调用Android原生ap...

  • Deeplink实践原理分析

    目录介绍 01.先看一个场景 02.什么是DeepLink 03.什么是Deferred DeepLink 04....

  • scheme协议

    先说下DeepLink(深度链接) DeepLink,又称深度链接、调起链接,是一套链接服务,用户点击链接可以跳转...

  • deepLink

    写在前面 我们在使用deepLink的流程是这样的:(1)在A应用内配置了B应用的URLScheme,那么我们就可...

  • deeplink

    deeplink 1.什么是deeplink? 简而言之,就是你在手机浏览器上面点击一个链接,可以跳转到另一个ap...

  • deepLink iOS 应用到自己APP 记录

    deepLink iOS 应用到自己APP 记录 1.了解deeplink 详细的介绍可以在网上查询,这里简单说一...

网友评论

      本文标题:DeepLink

      本文链接:https://www.haomeiwen.com/subject/llfbjrtx.html