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/)

相关推荐
雨白8 小时前
Jetpack系列(二):Lifecycle与LiveData结合,打造响应式UI
android·android jetpack
kk爱闹10 小时前
【挑战14天学完python和pytorch】- day01
android·pytorch·python
每次的天空12 小时前
Android-自定义View的实战学习总结
android·学习·kotlin·音视频
恋猫de小郭12 小时前
Flutter Widget Preview 功能已合并到 master,提前在体验毛坯的预览支持
android·flutter·ios
断剑重铸之日13 小时前
Android自定义相机开发(类似OCR扫描相机)
android
随心最为安13 小时前
Android Library Maven 发布完整流程指南
android
岁月玲珑13 小时前
【使用Android Studio调试手机app时候手机老掉线问题】
android·ide·android studio
还鮟17 小时前
CTF Web的数组巧用
android
小蜜蜂嗡嗡19 小时前
Android Studio flutter项目运行、打包时间太长
android·flutter·android studio
aqi0019 小时前
FFmpeg开发笔记(七十一)使用国产的QPlayer2实现双播放器观看视频
android·ffmpeg·音视频·流媒体