Android中的深度鏈接技術(shù)實(shí)戰(zhàn)
前言
日常中,我們經(jīng)常需要從瀏覽器中的網(wǎng)頁或者從其它APP中直接打開我們的APP,我們就需要使用到深度鏈接技術(shù)。實(shí)現(xiàn)方式分別是 Dee pLinks 和 APP Links。
Deep Links
deep links是谷歌支持的一種打開app指定頁面的方式,常用于從H5頁面跳轉(zhuǎn)至app目標(biāo)頁面。其對(duì)應(yīng)指定頁面的匹配規(guī)則是按照URI來匹配的。常見URI格式如下圖:

示例
- H5測(cè)試頁面
<html> <a rel="external nofollow" >點(diǎn)擊喚起app</a> <a rel="external nofollow" >點(diǎn)擊喚起app</a> <a href="abc://demo.deaven.com:2003/test/data?params1=value1¶ms2=value2" rel="external nofollow" >點(diǎn)擊喚起app</a> </html>
如上
- scheme = http、https、abc。 DeepLink中 scheme 可自定義
- host = demo.deaven.com
- port = 2003
- path = test/data
- 傳遞參數(shù)(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,此項(xiàng)可以不寫-->
<!-- 注意 "/" 在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¶ms2=value2
String value1 = uri.getQueryParameter("params1");
String value2 = uri.getQueryParameter("params2");
}為了更好的管理以及用戶體驗(yàn),app中可以聲明一個(gè)中間頁根據(jù)參數(shù)統(tǒng)一分發(fā)跳轉(zhuǎn)請(qǐng)求。
注意事項(xiàng)
scheme為 htttp/https 開頭的uri,部分瀏覽器和手機(jī)ROM 并不能鏈接至APP,而是在瀏覽器中打開了對(duì)應(yīng)的鏈接。所以做Deep Links時(shí)建議全部采用自定義Scheme的形式。
在詢問是否用APP打開對(duì)應(yīng)的鏈接時(shí),如果選擇了“取消”并且“記住選擇”被勾上,那么下次你再次想鏈接至APP時(shí)就不會(huì)有任何反應(yīng)!!!
不同的host不要寫在同一個(gè)Intent Filter中,最好為每種匹配規(guī)則新建一個(gè)Intent Filter
App Links
Android在Android 6.0 (API level 23) 及以后加入了App Links , 當(dāng)用戶點(diǎn)擊對(duì)應(yīng)的URI 時(shí),會(huì)直接啟動(dòng)對(duì)應(yīng)的APP,不會(huì)再出現(xiàn)類似Deep Links 中是否打開app 的對(duì)話框出現(xiàn)。
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,此項(xiàng)可以不寫-->
<!-- 注意 "/" 在pathPrefix中是必須的-->
<data android:pathPrefix="/test"/>
</intent-filter>
</activity>Intent Filter和Deep Links 類似 但是 scheme只能使用 htttp 或 https 不支持自定義scheme。
android:autoVerify="true" 會(huì)讓APP自動(dòng)在所列的host中去驗(yàn)證,如果驗(yàn)證成功,APP將成為匹配URI默認(rèn)打開方式。
配置 assetlinks.json
- 你可以訪問https://developers.google.com/digital-asset-links/tools/generator生成assetlinks.json,如下圖:

如不能翻墻,可復(fù)制下方代碼修改為自己參數(shù),生成 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檢查服務(wù)器上assetlinks.json是否可訪問如下圖:

3.Activity中解析Intents 類似 Deep Links
參考文檔
https://www.jianshu.com/p/1632be1c2451
到此這篇關(guān)于Android中的深度鏈接技術(shù)實(shí)戰(zhàn)的文章就介紹到這了,更多相關(guān)Android 深度鏈接內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
打飛機(jī)游戲終極BOSS Android實(shí)戰(zhàn)打飛機(jī)游戲完結(jié)篇
打飛機(jī)游戲終極BOSS,Android實(shí)戰(zhàn)打飛機(jī)游戲完結(jié)篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
Android 8.0 慢充和快充提示語的實(shí)現(xiàn)原理
這篇文章主要介紹了Android 8.0 慢充和快充提示語的實(shí)現(xiàn)原理,感興趣的朋友跟隨腳本之家小編一起看看吧2018-05-05
android H5本地緩存加載優(yōu)化的實(shí)戰(zhàn)
這篇文章主要介紹了android H5本地緩存加載優(yōu)化的實(shí)戰(zhàn),幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04
android自定義view實(shí)現(xiàn)鐘表效果
這篇文章主要為大家詳細(xì)介紹了android自定義view實(shí)現(xiàn)鐘表效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
Android Mms之:聯(lián)系人管理的應(yīng)用分析
本篇文章是對(duì)Android中的聯(lián)系人管理進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android中使用Gson解析JSON數(shù)據(jù)的兩種方法
Json是一種類似于XML的通用數(shù)據(jù)交換格式,具有比XML更高的傳輸效率;本文將介紹兩種方法解析JSON數(shù)據(jù),需要的朋友可以參考下2012-12-12
Android自定義實(shí)現(xiàn)可回彈的ScollView
這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)可回彈的ScollView,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
ListView的Adapter使用(綁定數(shù)據(jù)) 之 自定義每一項(xiàng)的布局去綁定數(shù)據(jù)
之前寫的綁定數(shù)據(jù)是只是簡(jiǎn)單的綁定了字符串,這次我們將一次綁定多條數(shù)據(jù)并且嘗試用自定義的布局。在這篇文章中首先講解的是用Hashmap 去綁定數(shù)據(jù),第二個(gè)例子,講解自定義布局然后綁定數(shù)據(jù)2013-06-06

