Android中的深度链接技术实战

作者:星星同学 时间:2023-09-20 11:27:18 

前言

日常中,我们经常需要从浏览器中的网页或者从其它APP中直接打开我们的APP,我们就需要使用到深度链接技术。实现方式分别是 Dee pLinks 和 APP Links。

Deep Links

deep links是谷歌支持的一种打开app指定页面的方式,常用于从H5页面跳转至app目标页面。其对应指定页面的匹配规则是按照URI来匹配的。常见URI格式如下图:

Android中的深度链接技术实战

示例

  • H5测试页面

<html>
<a href="http://demo.deaven.com:2003/test/data?params1=value1&params2=value2" rel="external nofollow" >点击唤起app</a>
<a href="https://demo.deaven.com:2003/test/data?params1=value1&params2=value2" rel="external nofollow" >点击唤起app</a>
<a href="abc://demo.deaven.com:2003/test/data?params1=value1&params2=value2" rel="external nofollow" >点击唤起app</a>
</html>

如上

  • scheme = http、https、abc。 DeepLink中 scheme 可自定义

  • host = demo.deaven.com

  • port = 2003

  • path = test/data

  • 传递参数(key-value): params1 : value1 params2 : value2

  • Android配置

<activity android:name=".MainActivity"
          android:exported="true"
          android:launchMode="singleTask">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
           <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:scheme="http" />
               <data android:scheme="https" />
               <data android:scheme="abc" />
               <data android:host="demo.deaven.com"/>
               <data android:port="2003"/>
               <!--表示匹配 Path 以/test 开头的uri,此项可以不写-->
               <!-- 注意 "/" 在pathPrefix中是必须的-->
               <data android:pathPrefix="/test"/>

</intent-filter>
       </activity>

3.Activity中解析Intents

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

Uri uri = getIntent().getData();
   String scheme = uri.getScheme(); // http、https、abc
   String host = uri.getHost(); // demo.deaven.com
   String path = uri.getPath(); // test/data
   String query = uri.getQuery(); // params1=value1&params2=value2
   String value1 = uri.getQueryParameter("params1");
   String value2 = uri.getQueryParameter("params2");
}

为了更好的管理以及用户体验,app中可以声明一个中间页根据参数统一分发跳转请求。

注意事项

  • scheme为 htttp/https 开头的uri,部分浏览器和手机ROM 并不能链接至APP,而是在浏览器中打开了对应的链接。所以做Deep Links时建议全部采用自定义Scheme的形式。

  • 在询问是否用APP打开对应的链接时,如果选择了&ldquo;取消&rdquo;并且&ldquo;记住选择&rdquo;被勾上,那么下次你再次想链接至APP时就不会有任何反应!!!

  • 不同的host不要写在同一个Intent Filter中,最好为每种匹配规则新建一个Intent Filter

App Links

Android在Android 6.0 (API level 23) 及以后加入了App Links , 当用户点击对应的URI 时,会直接启动对应的APP,不会再出现类似Deep Links 中是否打开app 的对话框出现。

Intent Filter

<activity android:name=".MainActivity"
          android:exported="true"
          android:launchMode="singleTask"
          android:autoVerify="true">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
           <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:scheme="http" />
               <data android:scheme="https" />
               <data android:host="demo.deaven.com"/>
               <data android:port="2003"/>
               <!--表示匹配 Path 以/test 开头的uri,此项可以不写-->
               <!-- 注意 "/" 在pathPrefix中是必须的-->
               <data android:pathPrefix="/test"/>

</intent-filter>
       </activity>
  • Intent Filter和Deep Links 类似 但是 scheme只能使用 htttp 或 https 不支持自定义scheme。

  • android:autoVerify="true" 会让APP自动在所列的host中去验证,如果验证成功,APP将成为匹配URI默认打开方式。

配置 assetlinks.json

  • 你可以访问https://developers.google.com/digital-asset-links/tools/generator生成assetlinks.json,如下图:

Android中的深度链接技术实战

如不能翻墙,可复制下方代码修改为自己参数,生成 assetlinks.json文件 ,json文件名只能是 assetlinks 不能自定义

[{
 "relation": ["delegate_permission/common.handle_all_urls"],
 "target" : { "namespace": "android_app", "package_name": "com.deaven.link",
              "sha256_cert_fingerprints": [""14:6D:E9:83:C5:73:06:50:D8:EE:B9:95:2F:34:FC:64:16:A0:83:42:E6:1D:BE:A8:8A:04:96:B2:3F:CF:44:E5""] }
}]

2.部署assetlinks.json

我们的host为demo.deaven.com,那么我们就需将assetlinks.json放到https://demo.deaven.com/.well-known/assetlinks.json并可以正常访问。你也可以在 https://developers.google.com/digital-asset-links/tools/generator检查服务器上assetlinks.json是否可访问如下图:

Android中的深度链接技术实战

3.Activity中解析Intents 类似 Deep Links

参考文档

https://www.jianshu.com/p/1632be1c2451

来源:https://juejin.cn/post/7077831040705757214

标签:Android,深度链接
0
投稿

猜你喜欢

  • C# 关于LoadLibrary的疑问详解

    2023-07-26 23:14:10
  • C#将指定目录所有文件名转换成小写的方法

    2023-01-25 23:21:03
  • 详解使用Maven开发Web应用详细步骤

    2023-08-06 14:18:20
  • Java8中Optional操作的实际应用

    2022-04-30 22:52:31
  • java中 Set与Map排序输出到Writer详解及实例

    2022-05-01 07:23:56
  • vs 中C#项目读取JSON配置文件的方法

    2022-09-22 15:04:05
  • C#基于Socket实现多人聊天功能

    2021-12-06 12:00:47
  • C# 创建控制台应用程序

    2023-10-09 07:27:37
  • Java继承的问题引导和测试代码

    2023-03-13 18:50:34
  • Mybatis批量操作sql写法示例(批量新增、更新)

    2022-12-06 15:19:16
  • C# 列表List的常用属性和方法介绍

    2022-09-04 07:52:05
  • Java C++题解leetcode 1684统计一致字符串的数目示例

    2023-04-23 09:06:31
  • C# Soap调用WebService的实例

    2021-06-18 20:27:08
  • Spring拦截器HandlerInterceptor接口代码解析

    2022-09-05 10:51:04
  • 简单学习Java抽象类要点及实例

    2021-10-11 09:08:12
  • C#使用foreach语句遍历二维数组的方法

    2023-01-19 22:06:05
  • Java队列篇之实现数组模拟队列及可复用环形队列详解

    2021-09-12 17:49:42
  • C#使用StopWatch获取程序毫秒级执行时间的方法

    2023-12-21 02:44:06
  • Java中Lambda表达式和函数式接口的使用和特性

    2023-06-20 20:05:42
  • 详解android系统的定制

    2022-03-14 11:20:19
  • asp之家 软件编程 m.aspxhome.com