如何处理不同活动的应用程序链接?

How to handle app links on different activities?

我有,比方说,域 example.com。它可以与 httpshttp 一起使用。我有 Android 客户提供此服务。我希望客户端能够打开多个 link 类型。他们在这里:

http://example.com
https://example.com
http://example.com/app
https://example.com/app

这四个应该在ListActivity上打开。但也有其他 link 像:

https://testask.com/i/__some_guid__
https://testask.com/item/__some_guid__
and relevant link without https

它们应该在另一个 activity 上打开,例如 DetailsActivity。目前我有以下 DetailsActivity 的意图过滤器:

<intent-filter
    android:autoVerify="true"
    tools:targetApi="m">
    <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="@string/root_host_endpoint"
         android:pathPattern="/i/.*" />
    <data
         android:host="@string/root_host_endpoint"
         android:pathPattern="/item/.*" />
</intent-filter>

看起来它工作正常。但我不明白,如何添加 MainActivity 意图过滤器以指向根主机 url 并且不与 DetailsActivity 意图过滤器重叠。

您可以有多个具有不同意图过滤器的活动。您可以使用 Action/Category/Data 区分您的意图过滤器。在您的情况下,您必须调整数据配置以使其处理不同的意图。因此 MainActivity/ListActivity 将具有与 DetailsActivity

不同的数据配置
<activity android:name=".ListActivity">
<intent-filter
android:autoVerify="true"
tools:targetApi="m">
<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="example.com"
     android:pathPattern="/*/.*" />
</intent-filter>
</activity>

<activity android:name=".DetailsActivity">
<intent-filter
android:autoVerify="true"
tools:targetApi="m">
<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="@string/root_host_endpoint"
     android:pathPattern="/i/.*" />
<data
     android:host="@string/root_host_endpoint"
     android:pathPattern="/item/.*" />
</intent-filter>
</activity>

好吧,经过一些实验,我发现以下 Intent 过滤器按预期工作:

        <intent-filter
            android:autoVerify="true"
            tools:targetApi="m">
            <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="@string/root_host_endpoint" />
            <data
                android:host="@string/root_host_endpoint"
                android:path="/" />
            <data
                android:host="@string/root_host_endpoint"
                android:path="/app" />
        </intent-filter>