Android中的深度鏈接技術(shù)實戰(zhàn)
前言
日常中,我們經(jīng)常需要從瀏覽器中的網(wǎng)頁或者從其它APP中直接打開我們的APP,我們就需要使用到深度鏈接技術(shù)。實現(xiàn)方式分別是 Dee pLinks 和 APP Links。
Deep Links
deep links是谷歌支持的一種打開app指定頁面的方式,常用于從H5頁面跳轉(zhuǎn)至app目標(biāo)頁面。其對應(yīng)指定頁面的匹配規(guī)則是按照URI來匹配的。常見URI格式如下圖:
示例
- H5測試頁面
<html> <a rel="external nofollow" >點擊喚起app</a> <a rel="external nofollow" >點擊喚起app</a> <a href="abc://demo.deaven.com:2003/test/data?params1=value1¶ms2=value2" rel="external nofollow" >點擊喚起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,此項可以不寫--> <!-- 注意 "/" 在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"); }
為了更好的管理以及用戶體驗,app中可以聲明一個中間頁根據(jù)參數(shù)統(tǒng)一分發(fā)跳轉(zhuǎn)請求。
注意事項
scheme為 htttp/https 開頭的uri,部分瀏覽器和手機ROM 并不能鏈接至APP,而是在瀏覽器中打開了對應(yīng)的鏈接。所以做Deep Links時建議全部采用自定義Scheme的形式。
在詢問是否用APP打開對應(yīng)的鏈接時,如果選擇了“取消”并且“記住選擇”被勾上,那么下次你再次想鏈接至APP時就不會有任何反應(yīng)!!!
不同的host不要寫在同一個Intent Filter中,最好為每種匹配規(guī)則新建一個Intent Filter
App Links
Android在Android 6.0 (API level 23) 及以后加入了App Links , 當(dāng)用戶點擊對應(yīng)的URI 時,會直接啟動對應(yīng)的APP,不會再出現(xiàn)類似Deep Links 中是否打開app 的對話框出現(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,此項可以不寫--> <!-- 注意 "/" 在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,如下圖:
如不能翻墻,可復(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ù)實戰(zhàn)的文章就介紹到這了,更多相關(guān)Android 深度鏈接內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android如何獲取系統(tǒng)通知的開啟狀態(tài)詳解
這篇文章主要給大家介紹了關(guān)于Android如何獲取系統(tǒng)通知開啟狀態(tài)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起看看吧啊。2017-08-08Android開發(fā)之文本內(nèi)容自動朗讀功能實現(xiàn)方法
這篇文章主要介紹了Android開發(fā)之文本內(nèi)容自動朗讀功能實現(xiàn)方法,結(jié)合實例形式分析了Android自動朗讀TTS功能的操作步驟、相關(guān)函數(shù)使用方法與注意事項,需要的朋友可以參考下2017-09-09Android布局中margin與padding的區(qū)別及說明
這篇文章主要介紹了Android布局中margin與padding的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01Framework源碼面試之a(chǎn)ctivity啟動流程
這篇文章主要為大家介紹了Framework源碼面試之a(chǎn)ctivity啟動流程實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09