什么是App Linking?

App Linking是一种支持Android、iOS、HarmonyOS、Web等多种平台的跳转链接,链接的目标内容可以是您想要推广的产品优惠活动,也可以是用户之间可以互相分享的应用原生内容。您可以在创建App Linking后将链接地址直接发送给用户,或者由用户在应用中动态生成App Linking并分享给其他用户,接收到App Linking的用户点击链接后即可跳转到指定页面。
当用户点击App Linking时:

您将建立什么?

在本次Codelab中,您将建立一个集成App Linking的Android应用程序。您可以在AGC上创建一个App Linking链接,然后在应用中点击此链接就可直接拉起App。

您将会学到什么?

开发环境及技能要求

运行终端要求

集成App Linking,需要完成以下准备工作:

具体操作,请按照《AppGallery Connect应用集成准备》中详细说明来完成。

开通App Linking服务

  1. AppGallery Connect页面点击"我的项目",在项目的应用列表中选择需要开通App Linking项目,导航选择"增长 > App Linking"。如果是首次使用App Linking服务,请点击"立即使用"开通服务。
  2. 如果产品数据处理位置未设置,需要选择该产品的数据处理位置,具体操作请参见设置数据处理位置

集成SDK

针对Android Studio开发环境,华为提供了maven仓集成方式的SDK包,开发前需集成SDK到您的Android Studio项目中。

  1. 在AppGallery Connect页面点击"我的项目",在项目的应用列表中选择需要开通App Linking的应用。
  2. 在项目设置的"常规"页签下,单击"应用"栏下的"agconnect-services.json"下载配置文件。
  3. 将"agconnect-services.json"文件拷贝到应用级根目录下。
  4. 打开Android Studio应用级build.gradle文件,在对应位置配置App Linking SDK。对App Linking的事件进行数据统计时需要使用华为分析服务,请同步添加华为分析服务SDK。
    // 配置如下地址 apply plugin: 'com.huawei.agconnect' dependencies { // 配置如下地址 implementation 'com.huawei.agconnect:agconnect-applinking:1.6.5.300' implementation 'com.huawei.hms:hianalytics:6.3.2.300' }
  5. 点击界面上的"Sync Now"链接同步已完成的配置。

本次Codelab中您可以在您的Android Studio工程中创建一个布局页面,参照下图进行UI设计,能够接收App Linking并且展示接收结果即可。

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to the Codelab" android:textSize="24dp" android:textAlignment="center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.1" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="getLinkingResult:" android:textSize="20sp" android:textAllCaps="false" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.3" /> <TextView android:id="@+id/result_text" android:layout_width="match_parent" android:layout_height="50dp" android:textSize="15dp" android:hint="Result" android:textAlignment="center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.4" /> </androidx.constraintlayout.widget.ConstraintLayout>
  1. 登录AppGallery Connect网站,点击"我的项目"。
  2. 在项目列表中找到您的项目,在项目下的应用列表中选择您的应用。
  3. 在导航选择"增长" >" App Linking",选择"链接前缀"页签,点击"添加链接前缀"。在"设置域名"中输入本次Codelab所使用的网址前缀。
  4. 完成输入并且点击"下一步",系统将自动验证该链接前缀是否可用。
  1. 在App Linking页面下,选择"App Linking"页签,点击"创建App Linking"
  2. 在"设置短链接"步骤中,直接使用系统推荐默认的即可,直接点击"下一步"。
  3. 在"设置深度链接"步骤,配置相应的参数,其中:
    1. 链接名称 :按需配置即可,一般配置为该链接的使用场景,此处配置为:AppLinking-easy。
    2. 深度链接地址(默认):一般配置为PC打开场景下可访问的H5地址。此处配置为:https://developer.huawei.com/consumer/cn。
    3. Android深度链接地址:Android设备点击该链接中,重定向并且获取到的地址。此处配置为:codelabeasy://applinking.codelab/testid=123。其他参数,暂时不填。直接点击下一步。
  4. 设置链接行为,在"设置Android链接行为中",将行为配置为"在Android应用中打开",并且下拉选择对应的包名,未安装应用时,选择"华为应用市场"详情页。
  5. 其他可选参数本次CodeLab不填,直接点击右上角的"发布"按钮,发布该App Linking链接。
  1. 打开app/src/main路径下的AndroidManifest文件,在MainActivity的<activity>标签中添加需要接收DeepLink域名的Intent过滤器。
    <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="applinking.codelab" android:scheme="codelabeasy" /> </intent-filter>
  2. 打开app/src/main/java/PACKAGE_NAME路径下的MainActivity文件,接收App Linking并显示App Linking中的DeepLink。
    AGConnectAppLinking.getInstance().getAppLinking(this).addOnSuccessListener(resolvedLinkData -> { Uri deepLink = null; if (resolvedLinkData!= null) { deepLink = resolvedLinkData.getDeepLink(); Log.i("AppLinkingCodeLab", "Open From App Linking: " + deepLink.toString()); TextView resultText = findViewById(R.id.result_text); resultText.setText(deepLink.toString()); // Perform subsequent processing based on the deep link } });
  1. 运行Android Studio工程生成APK包,并在测试手机中安装APK包。
  2. AppGallery Connect网站的App Linking项目下,找到步骤8中创建的App Linking链接,点击二维码。
  3. 打开手机中的浏览器,打开扫码功能扫描该二维码,即可实现App的拉起。

祝贺您,您已经成功地构建了您的第一个集成App Linking的应用程序,并学到了:

Code copied