• 如何在HarmonyOS应用中集成App Linking服务


     简介

    App Linking是一种不管应用是否已经安装都可以按照所需方式工作的跨平台链接,链接的目标内容可以是您想要推广的产品优惠活动,也可以是用户之间可以互相分享的应用原生内容。您可以在创建App Linking后将链接地址直接发送给用户,或者由用户在应用中动态生成App Linking并分享给其他用户,接收到App Linking的用户点击链接后即可跳转到链接指定的内容。

    当用户点击App Linking时:

    • 在Harmony平台上如果应用已经安装,打开App Linking会直接跳转的您应用的原生内容,如果应用没有安装,则会引导用户安装应用并跳转到链接的内容。

    • 在桌面浏览器上打开App Linking会转到网站上的同等内容。

    配置开发环境

    开通App Linking

    1. 在AppGallery Connect页面点击“我的项目”,在项目的应用列表中选择需要开通App Linking的应用,导航选择“增长 > App Linking”。如果是首次使用App Linking服务,请点击“立即开通”开通服务。

      1.png

    2. 如果产品数据处理位置未设置,需要选择该产品的数据处理位置,具体操作请参见设置数据处理位置

    集成SDK

    1. AppGallery Connect页面点击“我的项目”,在项目的应用列表中选择需要开通App Linking的应用。

    2. 在项目设置的“常规”页签下,单击“应用”栏下的“agconnect-services.json”下载配置文件。

    3. 将“agconnect-services.json”文件拷贝到项目entry模块目录下。

      image.png

    4. 打开entry模块下的build.gradle文件,添加如下代码集成App Linking SDK。

      1. dependencies {
      2. ...
      3. implementation 'com.huawei.agconnect:agconnect-applinking-harmony:1.1.0.300'
      4. ...
      5. }

    5. 点击界面上的“Sync Now”同步已经完成的配置。

    界面设计

    本次Codelab中您可以在您的DevEco Studio工程中创建一个布局页面,参照下图进行UI设计,具备简单的创建App Linking及分享App Linking按钮即可。

    image.png

    布局代码如下:

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <DirectionalLayout
    3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
    4. ohos:height="match_parent"
    5. ohos:width="match_parent"
    6. ohos:alignment="top"
    7. ohos:orientation="vertical">
    8. <Text
    9. ohos:id="$+id:AppLinking"
    10. ohos:height="match_content"
    11. ohos:width="match_content"
    12. ohos:layout_alignment="horizontal_center"
    13. ohos:top_margin="100"
    14. ohos:text_size="80"
    15. ohos:left_margin="60"
    16. ohos:right_margin="60"
    17. ohos:multiple_lines="true"
    18. ohos:text="AppLinking"
    19. />
    20. <Button
    21. ohos:id="$+id:createButton"
    22. ohos:height="match_content"
    23. ohos:width="match_content"
    24. ohos:layout_alignment="horizontal_center"
    25. ohos:top_margin="30vp"
    26. ohos:text_size="80"
    27. ohos:background_element="$graphic:background_ability_main"
    28. ohos:text="Create link"
    29. />
    30. <Text
    31. ohos:height="match_content"
    32. ohos:width="match_content"
    33. ohos:left_margin="60"
    34. ohos:right_margin="60"
    35. ohos:multiple_lines="true"
    36. ohos:text_size="60"
    37. ohos:text="Short link"
    38. />
    39. <Text
    40. ohos:id="$+id:shortlink"
    41. ohos:height="match_content"
    42. ohos:width="match_content"
    43. ohos:left_margin="60"
    44. ohos:right_margin="60"
    45. ohos:multiple_lines="true"
    46. ohos:text_size="40"
    47. />
    48. <Text
    49. ohos:height="match_content"
    50. ohos:width="match_content"
    51. ohos:top_margin="20"
    52. ohos:left_margin="60"
    53. ohos:right_margin="60"
    54. ohos:multiple_lines="true"
    55. ohos:text_size="60"
    56. ohos:text="Long link"
    57. />
    58. <Text
    59. ohos:id="$+id:longlink"
    60. ohos:height="match_content"
    61. ohos:width="match_content"
    62. ohos:top_margin="20"
    63. ohos:left_margin="60"
    64. ohos:right_margin="60"
    65. ohos:multiple_lines="true"
    66. ohos:text_size="40"
    67. />
    68. <Button
    69. ohos:id="$+id:shareLongButton"
    70. ohos:height="match_content"
    71. ohos:width="match_content"
    72. ohos:top_margin="30vp"
    73. ohos:layout_alignment="horizontal_center"
    74. ohos:text_size="80"
    75. ohos:background_element="$graphic:background_ability_main"
    76. ohos:text="Share long link"
    77. />
    78. <Button
    79. ohos:id="$+id:shareShortButton"
    80. ohos:height="match_content"
    81. ohos:width="match_content"
    82. ohos:top_margin="30vp"
    83. ohos:layout_alignment="horizontal_center"
    84. ohos:text_size="80"
    85. ohos:background_element="$graphic:background_ability_main"
    86. ohos:text="Share short link"
    87. />
    88. </DirectionalLayout>

    配置控制台链接前缀

    1. 登录AppGallery Connect网站,点击“我的项目”。

    2. 在项目列表中找到您的项目,在项目下的应用列表中选择您的应用。

    3. 在“增长”栏下找到“App Linking”,选择“链接前缀”页签,点击“添加链接前缀”,在“设置域名”中输入本次Codelab所使用的网址前缀。

      2.png

    4. 完成输入并且点击“下一步”,系统将自动验证该链接前缀是否可用。

      3.png

    设置链接前缀与DeepLink

    在应用客户端创建App Linking时,需要有指定的链接前缀与DeepLink地址。本次Codelab中我们使用提前配置好的链接前缀与DeepLink地址。

    开发App Linking

    1、在MainAbilitySlice中导入相关类。

    1. import com.huawei.agconnect.applinking.AppLinking;
    2. import com.huawei.agconnect.applinking.ResourceTable;
    3. import ohos.aafwk.ability.AbilitySlice;
    4. import ohos.aafwk.content.Intent;
    5. import ohos.aafwk.content.Operation;
    6. import ohos.agp.components.Button;
    7. import ohos.agp.components.Component;
    8. import ohos.agp.components.Text;
    9. import ohos.hiviewdfx.HiLog;
    10. import ohos.hiviewdfx.HiLogLabel;
    11. import ohos.utils.net.Uri;

    2、在onStart方法定义相关界面元素。

    1. Button createButton = (Button) findComponentById(ResourceTable.Id_createButton);
    2. Button shareLongButton = (Button) findComponentById(ResourceTable.Id_shareLongButton);
    3. Button shareShortButton = (Button) findComponentById(ResourceTable.Id_shareShortButton);
    4. createButton.setClickedListener(new Component.ClickedListener() {
    5. @Override
    6. public void onClick(Component component) {
    7. createLinking();
    8. }
    9. });
    10. shareLongButton.setClickedListener(new Component.ClickedListener() {
    11. @Override
    12. public void onClick(Component component) {
    13. shareLink(longLink.getText());
    14. }
    15. });
    16. shareShortButton.setClickedListener(new Component.ClickedListener() {
    17. @Override
    18. public void onClick(Component component) {
    19. shareLink(shortLink.getText());
    20. }
    21. });

    3、初始化App Linking对象实例。

    AGConnectAppLinking.getInstance();

    4、创建App Linking。

    1. public void createLinking(){
    2. AppLinking.Builder builder =
    3. AppLinking.newBuilder()
    4. .setIsShowPreview(true)
    5. .setUriPrefix(DOMAIN_URI_PREFIX)
    6. .setDeepLink(Uri.parse(DEEP_LINK))
    7. .setHarmonyLinkInfo(
    8. AppLinking.HarmonyLinkInfo.newBuilder()
    9. .setHarmonyDeepLink("agckit://helloWorld")
    10. .build())
    11. .setCampaignInfo(
    12. AppLinking.CampaignInfo.newBuilder()
    13. .setName("HDC")
    14. .setSource("Huawei")
    15. .setMedium("App")
    16. .build())
    17. //展示预览页详情
    18. .setPreviewType(AppLinking.LinkingPreviewType.SocialInfo)
    19. .setSocialCardInfo(
    20. AppLinking.SocialCardInfo.newBuilder()
    21. .setDescription("HDC")
    22. .setTitle("Huawei")
    23. .setImageUrl("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3862731350,3483076630&fm=26&gp=0.jpg")
    24. .build());
    25. LongUri = builder.buildAppLinking().getUri().toString();
    26. longLink.setText(LongUri);
    27. HiLog.info(hiLogLabel,"this is LongUri: " + LongUri);
    28. builder.buildShortAppLinking().addOnSuccessListener(shortAppLinking ->
    29. {
    30. ShortUri = shortAppLinking.getShortUrl().toString();
    31. shortLink.setText(ShortUri);
    32. HiLog.info(hiLogLabel,"this is ShortLink: " + ShortUri);
    33. }).addOnFailureListener(e -> {
    34. });
    35. }

    5、分享已创建的App Linking。

    1. public void shareLink(String appLinking){
    2. if (appLinking != null) {
    3. Intent intent = new Intent();
    4. Operation operation = new Intent.OperationBuilder().withUri(Uri.parse(appLinking)).build();
    5. intent.setOperation(operation);
    6. startAbility(intent);
    7. }
    8. }

    接收App Linking

    1、在接收App Linking前,需要在HarmonyOS工程的config.json配置文件中添加Ability的skills字段,用于指定处理哪些App Linking。当前,支持使用Deep Link来接收信息,您需要在处理App Linking的Ability下添加如下配置。

    1. "skills": [
    2. {
    3. "entities": [
    4. "entity.system.home",
    5. "android.intent.action.VIEW"
    6. ],
    7. "actions": [
    8. "action.system.home",
    9. "entity.system.default",
    10. "entity.system.browsable"
    11. ],
    12. "uris": [
    13. {
    14. "scheme": "agckit",
    15. "host": "www.example.com"
    16. }
    17. ]
    18. }
    19. ]

    2、在需要接收App Linking的Ability中调用AGConnectAppLinking.getInstance()初始化AGConnectAppLinking实例。

    1. AGConnectAppLinking.getInstance()
    2. .getAppLinking(getAbility())
    3. .addOnSuccessListener(
    4. resolvedLinkData -> {
    5. if(resolvedLinkData != null){
    6. StringBuilder stringBuffer = new StringBuilder();
    7. if(resolvedLinkData.getDeepLink() != null){
    8. stringBuffer.append(resolvedLinkData.getDeepLink().toString());
    9. //解析出deeplink后跳转到实际页面
    10. Intent intents = new Intent();
    11. intents.setUri(Uri.parse(resolvedLinkData.getDeepLink().toString()));
    12. startAbility(intents);
    13. }
    14. }
    15. })
    16. .addOnFailureListener(
    17. e -> {
    18. AGCLogger.e("ApplinkingAbilitySlice", "getAppLinking:onFailure", e);
    19. });

    打包测试

    1、在DevEco Studio上点击运行按钮,在手机或模拟器上运行应用。点击“Create link”,您可以看到SDK生成的短链接和长链接。

    image.png

    2、点击“Share long link”按钮,将链接分享至手机浏览器,浏览器将复制长链接并打开地址,加载出配置的App Linking内容。点击打开或下载应用按钮,即可跳转至应用市场进行下载,说明您通过SDK生成的链接可以打开对应的应用。短链接测试方法也相同。

    image.png

    参考文件

    详细的App Linking开发指南请参见App Linking服务开发指南

    App Linking的相关API请参见API参考

    本Codelab中所用Demo源码下载地址如下源码下载

    欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

  • 相关阅读:
    进入IT行业:选择前端开发还是后端开发?
    开源网络协议栈onps诞生记
    emqx5.0.6 开启redis认证,配置acl规则,离线监听(保姆级图文实战教程)
    网络编程-IP、TCP、UDP1(第二十一天)
    Spring框架系列(9) - Spring AOP实现原理详解之AOP切面的实现
    C#基础语法--运算符
    模块打包器Webpack详解!
    【Matplotlib绘制图像大全】(十六):Matplotlib绘制虚线折线图
    Google验证码,扫描绑定,SpringBoot+ vue
    先进制造aps专题九 中国aps行业分析
  • 原文地址:https://blog.csdn.net/weixin_44708240/article/details/125634619