当前位置:   article > 正文

AppLinks使用详解

applinks

1. 简介

官方介绍Android App Links内容是:

  1. Android App Links are a special type of deep link that allow your website URLs to immediately open the
  2. corresponding content in your Android app (without requiring the user to select the app).
  3. To add Android App Links to your app, define intent filters that open your app content using HTTP URLs (as
  4. described in [Create Deep Links to App Content]), and verify that you own both your app and the website
  5. URLs (as described in this guide). If the system successfully verifies that you own the URLs, the system
  6. automatically routes those URL intents to your app.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

意思就是AppLinks是一个特殊的DeepLink,它可以让你的应用和你的网站URL进行绑定,这样当你在点击你网站链接的时候(非浏览器中)就能调起你的App,而不是出现选择界面,使用方法如下
Create Deep Links to App Content
这种绑定不是在点击的时候才核对链接,下面会介绍在什么情况下核对这种绑定的。

2. 与DeepLink的区别

官方是这样介绍DeepLink的。

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

Deeplink是一个intent过滤器,他可以使用户直接进入某个Activity页面。但是有个不好的是当匹配到多个intent时就会弹一个让用户选择的框。官方给了下面一张图,而AppLinks就不会有这个弹框:
The disambiguation dialog
具体区别官方也列了以下:

ItemDeep linksApp links
Intent URLscheme http, https, or a custom schemeRequires http or https
Intent actionAny actionRequires android.intent.action.VIEW
Intent categoryAny categoryRequires android.intent.category.BROWSABLE and android.intent.category.DEFAULT
Link verificationNoneRequires a Digital Asset Links file served on you website with HTTPS
User experienceMay show a disambiguation dialog for the user to select which app to open the linkNo dialog; your app opens to handle your website links
CompatibilityAll Android versionsAndroid 6.0 and higher

3.使用步骤

官方给的步骤如下Handling Android App Links

3.1 在manifest中开启autoVerify
  1. <activity ...>
  2. <intent-filter android:autoVerify="true">
  3. <action android:name="android.intent.action.VIEW" />
  4. <category android:name="android.intent.category.DEFAULT" />
  5. <category android:name="android.intent.category.BROWSABLE" />
  6. <data android:scheme="http" android:host="www.example.com" />
  7. <data android:scheme="https" />
  8. </intent-filter>
  9. </activity>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

这里注意下,开启autoVerify的activity中的

3.2 支持多hosts的绑定。
  1. <intent-filter>
  2. ...
  3. <data android:scheme="https" android:host="www.example.com" />
  4. <data android:scheme="app" android:host="open.my.app" />
  5. </intent-filter>
  • 1
  • 2
  • 3
  • 4
  • 5

上面在同一个

3.3 在网站上创建assetlinks.json文件

具体格式如下:

  1. [{
  2. "relation": ["delegate_permission/common.handle_all_urls"],
  3. "target": {
  4. "namespace": "android_app",
  5. "package_name": "com.example",
  6. "sha256_cert_fingerprints":
  7. ["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"]
  8. }
  9. }]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

其中package_name就是应用的包名,sha256_cert_fingerprints为正式版的签名。上传时修改这两个属性值。如果一个应用对于多个网站时,可以配置多个对象,配置如下:

  1. [{
  2. "relation": ["delegate_permission/common.handle_all_urls"],
  3. "target": {
  4. "namespace": "android_app",
  5. "package_name": "com.example.puppies.app",
  6. "sha256_cert_fingerprints":
  7. ["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"]
  8. }
  9. },
  10. {
  11. "relation": ["delegate_permission/common.handle_all_urls"],
  12. "target": {
  13. "namespace": "android_app",
  14. "package_name": "com.example.monkeys.app",
  15. "sha256_cert_fingerprints":
  16. ["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"]
  17. }
  18. }]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

然后将该文件放在网站的.well-known目录下,放了之后要试试能不能用:

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

提交完后确定以下几个是否正确:

  1. Be sure of the following:
  2. * The `assetlinks.json` file is served with content-type `application/json`.
  3. * 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.
  4. * 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`).
  5. * 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).
  6. * 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

还有AppLinks仅支持https的网站。

3.4 测试AppLinks
  1. 测试json文件是否正确,请看 Statement List Generator and Tester
    也可以采用以下链接进行验证:
  1. https://digitalassetlinks.googleapis.com/v1/statements:list?
  2. source.web.site=https://domain.name:optional_port&
  3. relation=delegate_permission/common.handle_all_urls
  • 1
  • 2
  • 3
  1. 测试intent是否正确
    可以使用adb进行测试,命令如下:
  1. adb shell am start -a android.intent.action.VIEW \
  2. -c android.intent.category.BROWSABLE \
  3. -d "http://domain.name:optional_port"
  • 1
  • 2
  • 3

下面命令测试已经存在的绑定:

adb shell dumpsys package domain-preferred-apps
  • 1

上面命令等价于:

adb shell dumpsys package d
  • 1

如果存在绑定的会显示如下结果:

  1. Package: com.android.vending
  2. Domains: play.google.com market.android.com
  3. Status: always : 200000002
  • 1
  • 2
  • 3

参数含义如下:

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

4. 总结

  1. 优点:

    • 不会弹选择框
    • 可以直接通过url跳到对应的activity
  2. 缺点:

    • 网站需要支持https
    • 有个校验过程,步骤麻烦些。

使用该机制可以直接绕过intent方式,直接通过url就能打开对应的界面。不过在设置中还是能关闭这个。目前支持该功能的应用和网站还是很少。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/296758
推荐阅读
相关标签
  

闽ICP备14008679号