DeepLink官網(wǎng)上有這樣的解釋:
When a clicked link or programmatic request invokes a web URI intent, the Android system tries each of the following actions, in sequential order, until the request succeeds:
1. Open the user's preferred app that can handle the URI, if one is designated.
2. Open the only available app that can handle the URI.
3. Allow the user to select an app from a dialog.
Follow the steps below to create and test links to your content. You can also use the [App Links Assistant](https://developer.android.com/studio/write/app-link-indexing.html) in Android Studio to add Android App Links
翻譯后的意思就是:
1.當(dāng)單擊鏈接或編程請求調(diào)用Web URI意圖時廷臼,Android系統(tǒng)按順序依次嘗試以下每一個操作苍柏,直到請求成功為止:
2.打開用戶首選的應(yīng)用程序锭吨,它可以處理URI疙咸,如果指定的話问顷。
3.打開可以處理URI的惟一可用應(yīng)用程序幻赚。
允許用戶從對話框中選擇應(yīng)用程序。
意思也就是用戶可以自己寫一串字符串,系統(tǒng)會對該字符串進(jìn)行解析隘弊,然后調(diào)起注冊過相應(yīng)scheme的應(yīng)用,如果有多個注冊了荒适,那么就會彈出對話框讓用戶選擇梨熙。
Google官方給了一個樣例:search-samples
以下根據(jù)Android官方的deep-linking的樣例來說明如何使用。
<activity
android:name="com.example.android.GizmosActivity"
android:label="@string/title_gizmos" >
<intent-filter android:label="@string/filter_view_http_gizmos">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "http://www.example.com/gizmos” -->
<data android:scheme="http"
android:host="www.example.com"
android:pathPrefix="/gizmos" />
<!-- note that the leading "/" is required for pathPrefix-->
</intent-filter>
<intent-filter android:label="@string/filter_view_example_gizmos">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "example://gizmos” -->
<data android:scheme="example"
android:host="gizmos" />
</intent-filter>
</activity>
在上面有兩個<intent-filter ..>這兩個<intent-filter ..>只是在<data ..>上有所區(qū)別刀诬,但是官方仍然建議我們分開寫咽扇。比如:
<intent-filter>
...
<data android:scheme="https" android:host="www.example.com" />
<data android:scheme="app" android:host="open.my.app" />
</intent-filter>
當(dāng)注冊了<intent-filter..>后,便可以在Activity的中獲取其他應(yīng)用傳過來的intent值陕壹,具體調(diào)用如下:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
}
我們可以這樣設(shè)置scheme來通過鏈接拉活app
AndroidManifest中這樣寫:
<activity
android:name=".activity.TestUI"
android:launchMode="singleTask"
android:theme="@style/AppTheme" >
<intent-filter>
<data
android:host="www.test.com"
android:scheme="myscheme"/>
<!-- 必須加上該項,對一段數(shù)據(jù)執(zhí)行的“標(biāo)準(zhǔn)”操作-->
<action android:name="android.intent.action.VIEW"/>
<!-- 必須加上該項 -->
<category android:name="android.intent.category.DEFAULT"/>
<!-- 如果希望該應(yīng)用可以通過瀏覽器的連接啟動质欲,則添加該項 -->
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
</activity>
TestUI中這樣寫:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_test);
Uri data = getIntent().getData();
if (data != null) {
String host = data.getHost();
String scheme = data.getScheme();
String authority = data.getAuthority();
XxLog.d("host=" + host + ",scheme=" + scheme + ",auth=" + authority);
String type = data.getQueryParameter("type");
String name = data.getQueryParameter("name");
String url = data.getQueryParameter("url");
XxLog.d("type=" + type + ",name=" + name + ",url=" + url);
} else
XxLog.e("data is null");
}
網(wǎng)頁中這樣寫:
<!DOCTYPE html>
<html>
<head>
<title>DeepLink落地頁</title>
</head>
<body>
<a href="testscheme://www.test.com?type=green&url=111&name=zhangs">啟動程序測試頁面</a>
</body>
</html>
開發(fā)過程中可以保存為html文件用手機(jī)瀏覽器打開測試效果
上線后由后臺將這段代碼放在后臺頁面
如果需要在網(wǎng)頁中添加一條點擊直接跳到應(yīng)用市場的按鈕,可以這樣
<a href="market://details?id=com.your.packagename">打開你的app市場詳情頁</a>
另外有文章說可以再點擊啟動程序的地方加一條判斷糠馆,如果2秒后還沒有喚醒嘶伟,那么就認(rèn)為該設(shè)備上沒有該app,即自動跳轉(zhuǎn)到應(yīng)用市場又碌。我不是做前端的九昧,所以就不過多分析了。
參考文章:
http://www.reibang.com/p/d5db3d2def3b
https://blog.csdn.net/wangkeke1860/article/details/49850997
http://www.reibang.com/p/21380058d609