前言 知乎在手機瀏覽器打開,會有個 App 內打開的按鈕,點擊直接打開且跳轉到該詳情頁,是不是有點神奇,是如何做到的呢? 效果預覽 Uri Scheme 配置 intent filter AndroidManifest.xml 測試網頁 main 下新建 assets 文件,寫了簡單的 Html 網 ...
前言
知乎在手機瀏覽器打開,會有個 App 內打開的按鈕,點擊直接打開且跳轉到該詳情頁,是不是有點神奇,是如何做到的呢?
效果預覽
Uri Scheme
配置 intent-filter
AndroidManifest.xml
<activity android:name=".MainActivity">
<!-- 需要添加下麵的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:host="myhost"
android:path="/main"
android:port="1024"
android:scheme="myscheme" />
</intent-filter>
</activity>
測試網頁
main 下新建 assets 文件,寫了簡單的 Html 網頁用於 WebView 展示,來進行測試。
index.html:
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>這是一個 WebView</h1>
<a href="market://details?id=com.tencent.mm">open app with market</a>
<br/>
<br/>
<a href="myscheme://myhost:1024/main?key1=value1&key2=value2">open app with Uri Scheme</a>
<br/>
<br/>
</body>
</html>
Web View 載入:
webView.loadUrl("file:///android_asset/index.html");
目標頁面
接受參數,做相應的處理。
Intent intent = getIntent();
if (null != intent && null != intent.getData()) {
// uri 就相當於 web 頁面中的鏈接
Uri uri = intent.getData();
Log.e(TAG, "uri=" +uri);
String scheme = uri.getScheme();
String host = uri.getHost();
int port = uri.getPort();
String path = uri.getPath();
String key1 = uri.getQueryParameter("key1");
String key2 = uri.getQueryParameter("key2");
Log.e(TAG, "scheme=" + scheme + ",host=" + host
+ ",port=" + port + ",path=" + path
+ ",query=" + uri.getQuery()
+ ",key1=" + key1 + ",key2=" + key2);
}
列印消息如下:
uri=myscheme://myhost:1024/main?key1=value1&key2=value2
scheme=myscheme,host=myhost,port=1024,path=/main,query=key1=value1&key2=value2,key1=value1,key2=value2
原理
myscheme://myhost:1024/main?key1=value1&key2=value2,通過一個鏈接,為什麼能啟動相應的 APP 呢?Web 喚起 Android app 的實現及原理,一文說到關鍵代碼在 Android 6.0 的原生瀏覽器的 shouldOverrideUrlLoading 方法,核心實現在 UrlHandler 這個類中。代碼如下:
final static String SCHEME_WTAI = "wtai://wp/";
final static String SCHEME_WTAI_MC = "wtai://wp/mc;";
boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
if (view.isPrivateBrowsingEnabled()) {
// Don't allow urls to leave the browser app when in
// private browsing mode
return false;
}
if (url.startsWith(SCHEME_WTAI)) {
// wtai://wp/mc;number
// number=string(phone-number)
if (url.startsWith(SCHEME_WTAI_MC)) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(WebView.SCHEME_TEL +
url.substring(SCHEME_WTAI_MC.length())));
mActivity.startActivity(intent);
// before leaving BrowserActivity, close the empty child tab.
// If a new tab is created through JavaScript open to load this
// url, we would like to close it as we will load this url in a
// different Activity.
mController.closeEmptyTab();
return true;
}
//……
}
源碼
公眾號「吳小龍同學」回覆「SchemeSample」,獲取這次練習的完整示例。
Deep Links
如圖,在 Android M 之前,如果點擊一個鏈接有多個 APP 符合,會彈出一個對話框,詢問用戶使用哪個應用打開 - 包括瀏覽器應用。谷歌在Android M 上實現了一個自動認證(auto-verify)機制,讓開發者可以避開這個彈出框,使用戶不必去選擇一個列表,直接跳轉到他們的 APP。
創建
Android M App Links: 實現, 缺陷以及解決辦法
我沒有驗證,因為我玩不起來,有條件更新下 Deep Links 這塊內容,可以自己搭個本地伺服器。
弊端
需要 Android M
需要 Android 6.0(minSdkVersion 級別23)及更高版本上的才能使用。
.well-known/assetlinks.json
開發者必須維護一個與app相關聯的網站,通過在以下位置托管數字資產鏈接 JSON 文件來聲明您的網站與您的意圖過濾器之間的關係:
https://domain.name/.well-known/assetlinks.json
參考
Android移動開發者必須知道的Deep Linking技術
公眾號
我的公眾號:吳小龍同學,歡迎交流~