Android DeepLink使用指南

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末毕匀,一起剝皮案震驚了整個濱河市铸鹰,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌皂岔,老刑警劉巖蹋笼,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異凤薛,居然都是意外死亡姓建,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進(jìn)店門缤苫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來速兔,“玉大人,你說我怎么就攤上這事活玲』凉罚” “怎么了谍婉?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長镀钓。 經(jīng)常有香客問我穗熬,道長,這世上最難降的妖魔是什么丁溅? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任唤蔗,我火速辦了婚禮,結(jié)果婚禮上窟赏,老公的妹妹穿的比我還像新娘妓柜。我一直安慰自己,他們只是感情好涯穷,可當(dāng)我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布棍掐。 她就那樣靜靜地躺著,像睡著了一般拷况。 火紅的嫁衣襯著肌膚如雪作煌。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天赚瘦,我揣著相機(jī)與錄音粟誓,去河邊找鬼。 笑死起意,一個胖子當(dāng)著我的面吹牛努酸,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播杜恰,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼获诈,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了心褐?” 一聲冷哼從身側(cè)響起舔涎,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎逗爹,沒想到半個月后亡嫌,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡掘而,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年挟冠,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片袍睡。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡知染,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出斑胜,到底是詐尸還是另有隱情控淡,我是刑警寧澤嫌吠,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站掺炭,受9級特大地震影響辫诅,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜涧狮,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一炕矮、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧者冤,春花似錦吧享、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽钞它。三九已至拜银,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間遭垛,已是汗流浹背尼桶。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留锯仪,地道東北人泵督。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像庶喜,于是被迫代替她去往敵國和親小腊。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,592評論 2 353