【问题标题】:Intent-filter to match domain only (no path)仅匹配域的意图过滤器(无路径)
【发布时间】:2016-10-17 11:24:38
【问题描述】:

这可能看起来很简单,但我还不知道该怎么做。

当用户单击域 url 时,我需要打开我的应用程序。 我需要将 URL 与没有路径的域 (android:host) 匹配。 example.com 或 example.com/

我让它在 example.com/ 上工作(带有斜杠),但我无法捕获没有斜杠的 URL:example.com

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

以下是我的 Android 清单:

<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/

【问题讨论】:

    标签: android android-manifest intentfilter


    【解决方案1】:

    您不需要多个数据标签,在这种情况下您只需要一个。每个数据标签可以有一个方案、主机、路径、路径模式等。如果没有方案,则忽略其余方案。如果没有主机(但有方案),则忽略路径属性。做你想做的事情的正确方法是

    &lt;data android:scheme="http" android:host="example.com" /&gt;

    这就是你所需要的。 Docs

    【讨论】:

    • 这不起作用。如果没有路径参数,则仅匹配 http://example.com/,而不匹配 http://example.com(通过 adb shell start 进行测试)
    【解决方案2】:

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      • 2011-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多