只匹配域名而不匹配路径的Intent-filter

5

这似乎很简单,但我还没有想出如何做到。

我需要当用户点击域名 URL 时打开我的应用程序。 我需要将 URL 与域( android:host )(不带路径)进行匹配。 例如:example.com 或 example.com/

我成功地让它在 example.com/ (带斜杠) 上运行,但我无法捕获没有斜杠的 URL:example.com

我尝试使用 pathPattern /*,但它也不起作用。

以下是我的 Android Manifest:

<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="http" />
    <data android:scheme="https" />

    <data android:host="example.com" />
    <data android:host="www.example.com" />

    <data android:pathPattern="/*" />
    <data android:path="/" />
</intent-filter>

简单来说,我希望能够匹配具有或不具有结束斜杠“/”的URL。 http://example.com http://example.com/
2个回答

0

Android Studio的URL映射编辑器对我来说很具有误导性。它说https://example.org/anypath将根据以下配置映射到MainActivity,但实际上并不是这样。只有https://example.org/才会映射到MainActivity。

<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:host="www.example.org"
        android:path="/"
        android:scheme="https" />
</intent-filter>

-1

你不需要多个数据标签,只需要一个。每个数据标签可以有一套方案、主机、路径、路径模式等。如果没有方案,其余部分将被忽略。如果没有主机(但有方案),则忽略路径属性。做你想要的正确方法是:

<data android:scheme="http" android:host="example.com" />

这就是你所需要的全部。文档


这个不起作用。没有路径参数,只有 http://example.com/ 能匹配,而不是 http://example.com(通过 adb shell start 进行测试)。 - Cory Petosky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接