AppLinks使用詳解

1. 簡介

官方介紹Android App Links內(nèi)容是:

Android App Links are a special type of deep link that allow your website URLs to immediately open the
 corresponding content in your Android app (without requiring the user to select the app).

To add Android App Links to your app, define intent filters that open your app content using HTTP URLs (as 
described in [Create Deep Links to App Content]), and verify that you own both your app and the website 
URLs (as described in this guide). If the system successfully verifies that you own the URLs, the system 
automatically routes those URL intents to your app.

意思就是AppLinks是一個特殊的DeepLink,它可以讓你的應(yīng)用和你的網(wǎng)站URL進(jìn)行綁定,這樣當(dāng)你在點擊你網(wǎng)站鏈接的時候(非瀏覽器中)就能調(diào)起你的App,而不是出現(xiàn)選擇界面,使用方法如下
Create Deep Links to App Content
這種綁定不是在點擊的時候才核對鏈接,下面會介紹在什么情況下核對這種綁定的。

2. 與DeepLink的區(qū)別

官方是這樣介紹DeepLink的。

A [deep link] is an intent filter that allows users to directly enter a specific activity in your Android app. 
Clicking one of these links might open a disambiguation dialog, which allows the user to select one of 
multiple apps (including yours) that can hande the given URL. For example, figure 1 shows the 
disambiguation dialog after the user clicks a map link, asking whether to open the link in Maps or Chrome.

Deeplink是一個intent過濾器,他可以使用戶直接進(jìn)入某個Activity頁面。但是有個不好的是當(dāng)匹配到多個intent時就會彈一個讓用戶選擇的框。官方給了下面一張圖,而AppLinks就不會有這個彈框:

The disambiguation dialog

具體區(qū)別官方也列了以下:

Item Deep links App links
Intent URL scheme http, https, or a custom scheme Requires http or https
Intent action Any action Requires android.intent.action.VIEW
Intent category Any category Requires android.intent.category.BROWSABLE and android.intent.category.DEFAULT
Link verification None Requires a Digital Asset Links file served on you website with HTTPS
User experience May show a disambiguation dialog for the user to select which app to open the link No dialog; your app opens to handle your website links
Compatibility All Android versions Android 6.0 and higher

3.使用步驟

官方給的步驟如下Handling Android App Links

3.1 在manifest中開啟autoVerify

<activity ...>

    <intent-filter android:autoVerify="true">
        <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" android:host="www.example.com" />
        <data android:scheme="https" />
    </intent-filter>

</activity>

這里注意下,開啟autoVerify的activity中的<intent-filter..>的action必須為android.intent.action.VIEW,category必須包含android.intent.category.BROWSABLE,data的scheme必須包含http/https,否則不生效,而且AppLinks必須在Android 6.0 以上的手機才可生效。驗證包含以下幾方面:

  1. 系統(tǒng)會檢查包含以下幾方面的所有intent-filter
  • Action: android.intent.action.VIEW
  • Categories: android.intent.category.BROWSABLE and android.intent.category.DEFAULT
  • Data scheme: http or https
  1. 原話是:For each unique host name found in the above intent filters, Android queries the corresponding websites for the Digital Asset Links file at https://hostname/.well-known/assetlinks.json.。翻譯過來就是系統(tǒng)會讀取網(wǎng)站的/.well-known/assetlinks.json文件,然后驗證包名和簽名是否包含在assetlinks.json文件中。

當(dāng)且僅當(dāng)上面兩個條件滿足時才會形成綁定。

3.2 支持多hosts的綁定。

<intent-filter>
  ...
  <data android:scheme="https" android:host="www.example.com" />
  <data android:scheme="app" android:host="open.my.app" />
</intent-filter>

上面在同一個<intent-filter ..>里面寫的兩個<data ..>,他們除了組合https://www.example.comapp://open.my.appapp://www.example.comhttps://open.my.app也是滿足上面的<intent-filter ..>的。而分開寫的時候,不存在上面的問題。

3.3 在網(wǎng)站上創(chuàng)建assetlinks.json文件

具體格式如下:

[{
  "relation": ["delegate_permission/common.handle_all_urls"],
  "target": {
    "namespace": "android_app",
    "package_name": "com.example",
    "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"]
  }
}]

其中package_name就是應(yīng)用的包名,sha256_cert_fingerprints為正式版的簽名。上傳時修改這兩個屬性值。如果一個應(yīng)用對于多個網(wǎng)站時,可以配置多個對象,配置如下:

[{
  "relation": ["delegate_permission/common.handle_all_urls"],
  "target": {
    "namespace": "android_app",
    "package_name": "com.example.puppies.app",
    "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"]
  }
  },
  {
  "relation": ["delegate_permission/common.handle_all_urls"],
  "target": {
    "namespace": "android_app",
    "package_name": "com.example.monkeys.app",
    "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"]
  }
}]

然后將該文件放在網(wǎng)站的.well-known目錄下,放了之后要試試能不能用:

https://domain.name/.well-known/assetlinks.json

提交完后確定以下幾個是否正確:

Be sure of the following:

*   The `assetlinks.json` file is served with content-type `application/json`.
*   The `assetlinks.json` file must be accessible over an HTTPS connection, regardless of whether your app's intent filters declare HTTPS as the data scheme.
*   The `assetlinks.json` file must be accessible without any redirects (no 301 or 302 redirects) and be accessible by bots (your `robots.txt` must allow crawling `/.well-known/assetlinks.json`).
*   If your app links support multiple host domains, then you must publish the `assetlinks.json` file on each domain. See [Supporting app linking for multiple hosts](https://developer.android.com/training/app-links/verify-site-associations.html#multi-host).
*   Do not publish your app with dev/test URLs in the manifest file that may not be accessible to the public (such as any that are accessible accessible only with a VPN). A work-around in such cases is to [configure build variants](https://developer.android.com/studio/build/build-variants.html) to generate a different manifest file for dev builds

還有AppLinks僅支持https的網(wǎng)站。

3.4 測試AppLinks

  1. 測試json文件是否正確,請看 Statement List Generator and Tester
    也可以采用以下鏈接進(jìn)行驗證:
https://digitalassetlinks.googleapis.com/v1/statements:list?
   source.web.site=https://domain.name:optional_port&
   relation=delegate_permission/common.handle_all_urls
  1. 測試intent是否正確
    可以使用adb進(jìn)行測試,命令如下:
adb shell am start -a android.intent.action.VIEW \
    -c android.intent.category.BROWSABLE \
    -d "http://domain.name:optional_port"

下面命令測試已經(jīng)存在的綁定:

adb shell dumpsys package domain-preferred-apps

上面命令等價于:

adb shell dumpsys package d

如果存在綁定的會顯示如下結(jié)果:

Package: com.android.vending
Domains: play.google.com market.android.com
Status: always : 200000002

參數(shù)含義如下:

* Package - Identifies an app by its package name, as declared in its manifest.
* Domains - Shows the full list of hosts whose web links this app handles, using blank spaces as delimiters.
* Status - Shows the current link-handling setting for this app. An app that has passed verification, and 
whose manifest contains android:autoVerify="true", shows a status of always. The hexadecimal number after 
this status is related to the Android system's record of the user’s app linkage preferences. This value does 
not indicate whether verification succeeded.

4. 總結(jié)

  1. 優(yōu)點:
  • 不會彈選擇框
  • 可以直接通過url跳到對應(yīng)的activity
  1. 缺點:
  • 網(wǎng)站需要支持https
  • 有個校驗過程,步驟麻煩些。

使用該機制可以直接繞過intent方式,直接通過url就能打開對應(yīng)的界面。不過在設(shè)置中還是能關(guān)閉這個。目前支持該功能的應(yīng)用和網(wǎng)站還是很少。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,942評論 25 709
  • Intent組件雖然不是四大組件,但卻是連接四大組件的橋梁,學(xué)習(xí)好這個知識,也非常的重要。 一、什么是Intent...
    困惑困惑困惑閱讀 1,715評論 0 0
  • afinalAfinal是一個android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,858評論 2 45
  • 天使的時空
    眾心無相閱讀 244評論 0 0
  • 今天二月七號,這是家鄉(xiāng)下的第三場雪。
    白芷彌閱讀 260評論 1 0

友情鏈接更多精彩內(nèi)容