Android DeepLink 技术

来源:互联网 发布:查看公司开放端口 编辑:程序博客网 时间:2024/06/09 21:31

DeepLink 从字面意思可以理解为「深度链接」,那么 DeepLink 在 Android 开发中有什么作用呢?简单来说,可以用这种技术实现 web 页面点击一个链接跳转至 APP 指定的某一页面。这种技术的好处是可以为我们的 APP 导流。举个例子,我们公司是做一个房产平台的,如果用户在浏览器上看到新房列表的信息,并且手机上已经安装了应用的话,便可以直接跳转到 APP 的房产列表页面,原生页面在体验和功能上当然是要比 H5 页面强多了。如果此时没有安装应用可以直接跳转至下一个 H5 页面或者提示用户跳转到应用市场下载应用。

我们的应用使用的是 Scheme 的方式来实现的,废话不多说,来看看正确的使用姿势。以我自己的项目为例。我们需要在清单文件 manifest 中添加 intent-filter。

<activity      android:name=".common.activity.WebStarterActivity"      android:launchMode="singleTask">        <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="app.xxx.com"                android:scheme="openxxx" />            <data                android:host="app.xxx.com"                android:scheme="tel" />        </intent-filter></activity>

在对应 Activity 中的代码如下

if (getIntent() != null && getIntent().getData() != null) {            String refer = getIntent().getData().getQueryParameter("refer");            String path = getIntent().getData().getPath();            ...}

中间省去了业务逻辑代码,看到这里大家可能不太明白 Activiy 中的代码是什么意思,这里要简单的说一下 URL Scheme 协议的格式。

URL Scheme 协议格式

以我自己项目中定的协议为例,一个完整的 URL Schema 协议格式:

openxxx://app.xxx.com/newhouselist?city_id=xxxx&refer=xxxx

上面包含了 scheme、host、path、query。

  • openxxx 代表该 Scheme 协议名称(scheme)
  • app.xxx.com 代表 Scheme 作用于哪个地址域(host)
  • newhouselist 代表 Scheme 指定的页面(path)
  • city_id 代表传递的参数(query)

然后我们可以在 Activity 中获取 path 再跳转至对应的页面,并且可以通过 getQueryParameter() 方法来获取参数来传递给要跳转的页面。

测试

我们可以使用 adb 命令来测试是否能够调起 APP

adb shell am start -a android.intent.action.VIEW \\n-d "openxxx://app.xxx.com/newhouselist?city_id=11" com.xxx.android.app (这里填写 APP 的包名)

总结

DeepLink 的作用还是很大的,这点毋庸置疑,可以为我们的应用导流,提高用户体验。这里使用的是 Scheme 的方式来实现的,还有一种方式是 Android M 的 AppLinks,两者之间是有区别的,AppLinks 还没有使用和研究过,下次研究了之后再总结一下吧。在使用的过程中还是遇到了一些坑的,比如有的浏览器点击链接之后无法跳转,这些浏览器兼容的问题就需要做 web 页面的小伙伴去解决了。

0 0
原创粉丝点击