Android Deep Links 深度链接解析

在实现 Android 应用链接之前,请务必了解您可以在 Android 应用中创建的不同类型的链接:深层链接、网页链接和 Android 应用链接。

    • [一、什么是Deep Links?](#一、什么是Deep Links?)
    • [二、Deep Links的优势](#二、Deep Links的优势)
    • [三、Deep Links的实现方式](#三、Deep Links的实现方式)
      • [1. 显式Intent](#1. 显式Intent)
      • [2. 隐式Intent](#2. 隐式Intent)
      • [3. 使用示例](#3. 使用示例)
    • [四、Android App Links](#四、Android App Links)
        • [配置Android App Links](#配置Android App Links)
    • 五、总结

一、什么是Deep Links?

Deep Links,即深度链接,是一种能够让用户直接跳转到应用内特定页面的链接。它就像是一扇通往应用内部世界的门,用户通过点击一个链接,就能快速定位到自己想要的内容,而无需从应用首页开始逐级查找。

二、Deep Links的优势

  • 提升用户体验: 用户无需繁琐的操作,直接进入应用的特定页面,极大提高了用户体验。
  • 提高转化率: 通过Deep Links,可以将用户从外部渠道(如社交媒体、邮件)直接引导到应用内的购买页面、活动页面等,从而提高转化率。
  • 增强用户粘性: Deep Links可以帮助用户快速找到感兴趣的内容,增加用户对应用的粘性。

三、Deep Links的实现方式

Android Deep Links主要有两种实现方式:

1. 显式Intent

显式Intent指定了要启动的组件(Activity或Service)的明确类名。这种方式适用于我们完全控制应用内部跳转的情况。

java 复制代码
Intent intent = new Intent(this, DetailActivity.class);
intent.putExtra("itemId", 123);
startActivity(intent);

2. 隐式Intent

隐式Intent只指定了要执行的动作和数据类型,系统会根据这些信息找到最合适的组件来处理。这种方式常用于接收来自外部应用的Intent。

java 复制代码
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://example.com/product/123"));
startActivity(intent);

3. 使用示例

提前定义好自己的scheme、host等信息配置到清单文件里面,scheme是必须要有的,像hostpathPrefix等信息可以配置也可以没有,我这里配置了scheme和host两个条件,其中sheme是"appstore",host是"details",清单文件配置如下:

bash 复制代码
<activity
    android:name=".AppDetailActivity"
    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" />
        <data
            android:host="details"
            android:scheme="appstore" />
	</intent-filter>
</activity>

使用DeepLinks实现跳转,需要构建一个如下的Uri

bash 复制代码
appstore://details?id=com.xunlei.browser&name=APPNAME&package=com.example.app
java 复制代码
Uri appUri = new Uri.Builder()
        .scheme("appstore")
        .authority("details")
        .appendQueryParameter("id", "com.xunlei.browser")
        .appendQueryParameter("fromName", context.getString(R.string.app_name))
        .appendQueryParameter("fromPackage", context.getPackageName())
        .build();
Intent intent = new Intent(Intent.ACTION_VIEW, appUri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

或者使用 adb 工具进行测试

bash 复制代码
adb shell am start -W -a android.intent.action.VIEW -d "appstore://details?id=com.xunlei.browser&name=APPNAME&package=com.example" com.example.app

在首页Activity的onCreate方法和onNewIntent方法里面,接收Intent参数进行相应的跳转处理

java 复制代码
private void schemeIntent(Intent intent) {
       if (intent == null || intent.getData() == null) {
           return;
       }
       //获取Uri
       Uri uri = intent.getData();

       //打印出uri里取出的Scheme和Host
       Log.e("schemeIntent", "getScheme:" + uri.getScheme());
       Log.e("schemeIntent", "getHost:" + uri.getHost());

       //判断取出的Scheme和Host是否和自己配置的一样,如果一样进行相应的处理,否则不处理
       if (!SCHEME_VALUE.equals(uri.getScheme()) || !HOST_VAULE.equals(uri.getHost())) {
           return;
       }

       //如果Scheme和Host匹配成功,取出uri中的参数并进行相应的业务处理
       String id = uri.getQueryParameter("id");
       String name = uri.getQueryParameter("name");
       String package = uri.getQueryParameter("package");

       //打印uri里取出的参数
       Log.e("schemeIntent", "id :" + id );
       Log.e("schemeIntent", "name :" + name );
       Log.e("schemeIntent", "package:" + package);
}

Android App Links是Google提供的一套机制,用于在多个应用之间建立深层链接。它能够让用户在点击一个链接时,直接跳转到安装在设备上的对应应用,而无需选择。

  • 在AndroidManifest.xml中声明intent filter:
xml 复制代码
<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="example.com"
          android:pathPrefix="/product/"/>
</intent-filter>
  • 在Digital Assets Links文件中声明网站与应用之间的关联:
json 复制代码
{
  "@context": "https://www.digitalassetlinks.org",
  "relation": [
    {
      "target": {
        "namespace": "android-app",
        "package_name": "com.example.myapp",
        "sha256_cert_fingerprints": ["AB:CD:EF:12:34:56:78:90:AB:CD:EF:12:34:56:78:90"]
      },
      "assertion": [
        {
          "include": {
            "pathPrefix": "/product/"
          }
        }
      ]
    }
  ]
}

五、总结

Deep Links是Android开发中非常重要的一项技术,它可以极大地提升用户体验,提高应用的转化率。通过本文的介绍,相信大家对Deep Links有了更深入的了解。在实际开发中,我们可以结合不同的场景,灵活运用Deep Links,为用户打造更好的应用体验。

注意: 这是一篇基础的Deep Links入门教程,涉及到的内容还有很多,比如延迟深度链接、自定义URL Scheme等。建议大家深入阅读Android官方文档,了解更多细节。

相关参考:

1\] [Android上的Deep-Link技术调研](https://dorck.cn/android/2023/09/16/deep-link/)

相关推荐
莞凰5 小时前
昇腾CANN的“灵脉根基“:Runtime仓库探秘
android·人工智能·transformer
NiceCloud喜云6 小时前
Claude Files API 深入:从上传、复用到配额管理的工程化指南
android·java·数据库·人工智能·python·json·飞书
ujainu6 小时前
CANN pto-isa:虚拟指令集如何连接编译与执行
android·ascend
赏金术士7 小时前
第六章:UI组件与Material3主题
android·ui·kotlin·compose
TechMerger8 小时前
Android 17 重磅重构!服役 20 年的 MessageQueue 迎来无锁改造,卡顿大幅优化!
android·性能优化
yuhuofei202111 小时前
【Python入门】Python中字符串相关拓展
android·java·python
dalancon11 小时前
Android Input Spy Window
android
dalancon13 小时前
InputDispatcher派发事件,查找目标窗口
android
我命由我1234513 小时前
Android Framework P3 - MediaServer 进程、认识 ServiceManager 进程
android·c语言·开发语言·c++·visualstudio·visual studio·android runtime
天才少年曾牛14 小时前
Android14 新增系统服务后,应用调用出现 “hidden api” 警告的原因与解决方案
android·frameworks