移动开发:DeepLink(浏览器点击链接跳转到app)
2017-09-13 15:26
429 查看
DeepLink 是什么
DeepLink 从字面意思可以理解为「深度链接」,那么 DeepLink 在 Android 开发中有什么作用呢?简单来说,可以用这种技术实现 web 页面点击一个链接跳转至 APP 指定的某一页面。这种技术的好处是可以为我们的 APP 导流。举个例子,我们公司是做一个房产平台的,如果用户在浏览器上看到新房列表的信息,并且手机上已经安装了应用的话,便可以直接跳转到APP 的房产列表页面,原生页面在体验和功能上当然是要比 H5 页面强多了。如果此时没有安装应用可以直接跳转至下一个 H5 页面或者提示用户跳转到应用市场下载应用。
如何使用 DeepLink
在AndroidManifest.xml中设置
添加权限
<uses-permission android:name="android.permission.INTERNET"/>1
在Activity中添加intent-filter
<intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.BROWSABLE"/> <category android:name="android.intent.category.DEFAULT"/> <data android:scheme="test" android:host="tp" android:pathPrefix="/open"/> </intent-filter>1
2
3
4
5
6
7
scheme,host,pathPrefix可以根据自己需要设置。
浏览器要跳转到此Activity,需要url格式为:test://tp/open
可以在后面添加额外参数,比如:test://tp/open?name=lucy&age=8
Activity不一定是首先打开的Activity。(比如有ActivityB界面,可以在ActivityB中添加上面的intent-filter)
完整AndroidManifest.xml格式如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.git.webviewdemo"> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <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.BROWSABLE"/> <category android:name="android.intent.category.DEFAULT"/> <data android:scheme="test" android:host="tp" android:pathPrefix="/open"/> </intent-filter> </activity> </application> </manifest>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
网页代码
<html> <head> <meta name="meta_name" content="helloworld"> </head> <body> <a href="test://tp?name=miss&age=8">启动应用程序</a>//点击无法跳转 <a href="test://tp/open?name=miss&age=8">启动应用程序</a>//点击可以跳转 <a href="test://tp.app/open?name=miss&age=8">启动应用程序</a>//点击无法跳转 </body> </html>1
2
3
4
5
6
7
8
9
10
要确保url的链接与test://tp/open匹配才行。
此外不能在浏览器中输入,只能是通过网页点击有效。
Activity的对应操作
Intent intent = getIntent(); String action = intent.getAction(); if(Intent.ACTION_VIEW.equals(action)){ Uri uri = intent.getData(); if(uri != null){ String name = uri.getQueryParameter("name"); String age= uri.getQueryParameter("age"); Log.i("view_data", "name:" + name + " age:" + age); } }1
2
3
4
5
6
7
8
9
10
在Activity的onCreate方法中,通过上面代码进行数据的解析,及进一步操作。
一些补充
可以只写scheme
<data android:scheme="test"/>1
外部链接是test://格式的都会打开相应app。
对于传递参数
参数key=value格式,可以考虑把value进行encode。 app可以获取以后,进行一下decode。
跳转app时如果没有设置背景可能会黑屏或者白屏一段时间。
解决办法是:可以给跳转的app设置一个背景色。参考:应用启动白屏
相关文章推荐
- DeepLink(浏览器点击链接跳转到app)
- [Phonegap+Sencha Touch] 移动开发58 从移动浏览器(或webview)网页上点击链接跳转到QQ聊天界面
- 【代码】如何在手机等移动平台上浏览器网页中点击链接跳转到QQ聊天界面
- 安卓仿手机网易新闻app项目开发系列之(六)点击页面跳转和网络路径拼接
- android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据
- Android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据
- Android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据
- android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据
- android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据
- iOS点击短信中的链接跳转到App
- Android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据
- Android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据
- android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据
- Android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据
- Android 通过URL scheme 实现点击浏览器中的URL链接,启动特定的App,并调转页面传递参数
- Android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据
- Android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据
- android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据
- 微信浏览器内打开App Store链接,并跳转到对应的app页面
- Android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据