【问题标题】:Issue regarding resolution of incoming intents by intent filter关于通过意图过滤器解决传入意图的问题
【发布时间】:2020-05-28 16:05:08
【问题描述】:

遇到有关通过意图过滤器解决传入意图的问题,并想知道这种行为的确切原因。

我有一个具有如下意图过滤器的活动

 <activity android:name=".deeplink.DeeplinkActivity">
        <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="*"
                android:scheme="dltest" />

        </intent-filter>
    </activity>

一切都按预期工作,即它确实解析了 dltest://test1

格式的 URI

然后我必须支持 https://www.mine.com/ 格式的 URL 为此,我向相同的意图过滤器添加了一个新的数据标签,导致

  <activity android:name=".deeplink.DeeplinkActivity">
        <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.mine.com"
                android:scheme="https" />

            <data
                android:host="*"
                android:scheme="dltest" />

        </intent-filter>
    </activity>

现在它确实处理这两种类型的 URI,但它也处理所有意图,方案为 https,主机为未指定格式的任何内容(通配符),例如:https://developer.android.com

<data
     android:host="*"
     android:scheme="https" />

我知道可以通过在同一个活动中将两个数据标签分成两个不同的意图过滤器来解决这个问题,但我无法弄清楚上述解析策略是如何在任何地方工作的。谁能帮我解释一下原因

【问题讨论】:

  • 相同类型的多个元素导致条件的逻辑或。因此,您的两个&lt;category&gt; 元素意味着带有DEFAULT BROWSABLEIntent 将匹配Intent。您的实验表明,&lt;data&gt; 的逻辑 OR 是由单个属性决定的。因此,具有https dltestwww.mine.com * 的主机的Intent 将匹配。这只是一个猜测——我已经很长时间没有尝试过这种多个&lt;data&gt; 元素的样式了。

标签: android android-intent intentfilter


【解决方案1】:

Google 在其官方 android 文档之一中表示

虽然可以在同一个文件中包含多个元素 过滤器,重要的是您创建单独的过滤器时 目的是声明唯一的 URL(例如 方案和主机),因为同一意图中的多个元素 过滤器实际上被合并在一起以解释所有变化 它们的组合属性。例如,考虑以下情况:

<intent-filter> 
  <data android:scheme="https" android:host="www.example.com" />
  <data android:scheme="app" android:host="open.my.app" />
</intent-filter>

这似乎只支持https://www.example.com 和app://open.my.app。但是,它实际上支持这两个,以及这些:app://www.example.com 和 https://open.my.app

所以基本上正如@commonsware 所说,它在幕后执行有点逻辑OR ...从而确保满足所有组合

来源:Create Deep Links to App Content

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-07-27
  • 1970-01-01
  • 2015-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
相关资源
最近更新 更多