为应用链接添加 intent 过滤器

应用链接是使用 HTTP 或 HTTPS 架构的深层链接,并且已通过 Android 验证与您的网站相关联。如需注册以处理应用链接,请按以下步骤操作:

  1. 在应用清单中添加一个或多个指定网站网域或网址的 intent 过滤器。
  2. autoVerify="true"attribute 添加到 intent 过滤器元素。这会向系统发出信号,表明系统应尝试根据您网站的 assetlinks.json 配置验证方案和主机网域。
  3. 声明网站关联性。

以下是应用链接声明的示例,其中包含方案和主机以及 autoVerify="true

<activity
    android:name=".MainActivity"
    android:exported="true"
    ...>
    <!-- Make sure you explicitly set android:autoVerify to "true". -->
    <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" />

        <!-- If a user clicks on a link that uses the "http" scheme, your
             app should be able to delegate that traffic to "https". -->
        <!-- Do not include other schemes, as this will prevent verification. -->
        <data android:scheme="http" />
        <data android:scheme="https" />

        <!-- Include one or more domains that should be verified. -->
        <data android:host="www.example.com" />
        <data android:host="*.example.com" />
    </intent-filter>
</activity>

代码要点

  • AutoVerify:应用链接必须设置 android:autoVerify="true" 属性。用于向系统表明,它应尝试验证应用与 <data> 标记中指定的方案和网域之间的关联。建议为每个要验证的 Intent 过滤器添加 autoVerify="true
  • 数据元素:每个应用链接 intent 过滤器都必须包含一个或多个 <data> 元素,用于指定与可验证的网站网域匹配的方案和主机格式。
  • 方案:intent 过滤器必须包含 httphttps 方案的 <data> 元素。
  • 主机:您可以选择添加 <data> 元素来匹配一个或多个主机。使用通配符 (*) 匹配多个子网域(例如 *.example.com)。系统会尝试根据您网站上的 assetlinks.json 文件验证每个主机。请注意,任何路径级路由都应由 assetlinks.json 文件处理(请参阅下方的最佳实践部分)。

  • 多个主机:如果您声明了多个主机网域,系统(在 Android 12 及更高版本上)会尝试验证每个网域。如果任何主机通过验证,应用将成为来自该已验证主机的链接的默认处理程序。在 Android 11 及更低版本中,如果即使只有一个主机无法验证,验证也会失败。

  • 多个 intent 过滤器:如果您打算声明唯一的网址(例如架构和主机的特定组合),请务必创建单独的过滤器,因为同一 intent 过滤器中的多个 <data> 元素会合并在一起,以涵盖合并后属性的所有变体。

清单过滤规则注意事项

如果您要在 Android 15 及更高版本中设置过滤器以搭配动态应用链接使用,请务必注意,在服务器端 assetlinks.json 文件中声明的动态规则无法扩大您在应用清单中静态声明的网址规则的范围。

因此,我们建议您使用以下方法:

  • 在应用清单中,设置尽可能广泛的范围,例如仅声明方案和网域
  • 依靠服务器端 assetlinks.json 规则进行进一步细化,例如路径级路由。

借助此理想配置,您将能够根据需要在 assetlinks.json 文件中动态添加新的应用链接路径,同时知道这些路径将符合您在应用清单中设置的广泛范围。

支持多个主机的应用链接

系统必须能够对照相应 intent 过滤器中各个网域上托管的 Digital Asset Links 文件验证应用网址 intent 过滤器的数据元素中指定的主机。如果验证失败,系统会默认采用标准行为来解析相应 intent,具体如创建指向应用内容的深层链接中所述。不过,该应用仍可被验证为应用其他 intent 过滤器中定义的任何网址格式的默认处理程序。

例如,如果在 https://www.example.com/.well-known/assetlinks.json 中找到 assetlinks.json 文件,但在 https://www.example.net/.well-known/assetlinks.json 中未找到,则具有以下 intent 过滤器的应用仅会通过针对 https://www.example.com 的验证:

<application>

  <activity android:name=”MainActivity”>
    <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" />
      <data android:scheme="https" />
      <data android:host="www.example.com" />
    </intent-filter>
  </activity>
  <activity android:name="SecondActivity">
    <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:scheme="https" />
     <data android:host="www.example.net" />
    </intent-filter>
  </activity>

</application>

支持多个子网域的应用链接

Digital Asset Links 协议将 intent 过滤器中的子网域视为唯一的独立主机。因此,如果您的 intent 过滤器列出多个包含不同子网域的主机,您必须在每个网域上分别发布一个有效的 assetlinks.json。 例如,以下 intent 过滤器包含 www.example.commobile.example.com 作为接受的 intent 网址主机。因此,必须在 https://www.example.com/.well-known/assetlinks.jsonhttps://mobile.example.com/.well-known/assetlinks.json 上发布有效的 assetlinks.json

<application>
  <activity android:name="MainActivity">
    <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="https" />
      <data android:scheme="https" />
      <data android:host="www.example.com" />
      <data android:host="mobile.example.com" />
    </intent-filter>
  </activity>
</application>

或者,如果您使用通配符声明主机名(例如 *.example.com),则必须在根主机名 (example.com) 发布 assetlinks.json 文件。例如,只要将 assetlinks.json 文件发布到 https://example.com/.well-known/assetlinks.json,具有以下 intent 过滤器的应用便会通过针对 example.com 的任何子域名(例如 foo.example.com)的验证:

<application>
  <activity android:name="MainActivity">
    <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="https" />
      <data android:host="*.example.com" />
    </intent-filter>
  </activity>
</application>

检查是否有多个应用与同一网域相关联

如果您发布了多个应用,其中每个应用都与同一网域相关联,则它们都可以成功地通过验证。不过,如果这些应用可以解析完全相同的网域主机和路径,就像一个应用的精简版和完整版一样,则只有最近安装的应用才能解析该网域的网络 intent。

在这种情况下,请检查用户的设备上是否有可能发生冲突的应用,前提是具有必要的软件包可见性。然后,在您的应用中显示一个自定义选择器对话框,其中包含调用 queryIntentActivities 所得到的结果。用户可以从该对话框中显示的匹配应用的列表中选择他们偏好的应用。