【问题标题】:Why is my intent-filter matching URIs it shouldn't?为什么我的意图过滤器不应该匹配 URI?
【发布时间】:2014-06-26 21:48:00
【问题描述】:

我的 android 应用有一个像这样的意图过滤器:

  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <action android:name="android.intent.action.SENDTO" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="satur9nine" android:host="*" />
    <data android:scheme="http" android:host="www.satur9nine.com" android:pathPrefix="/app" />
  </intent-filter>

它应该匹配 satur9nine://anything 或 http://www.satur9nine.com/app/anything。但是它匹配http://www.notmywebsite.com/app,有什么问题?

【问题讨论】:

    标签: android android-intent intentfilter


    【解决方案1】:

    这方面的文档相当模糊,但您可以通过在IntentFilter 文档中看到addDataSchemeaddDataPathaddDataAuthority 方法都相互独立并且没有办法将方案、路径和权限一起添加。

    查看IntentFilter source 即可确认。数据 URI 的每个部分(模式、路径、权限)都存储在自己的列表中,因此来自不同 &lt;data&gt; 元素的值在匹配代码运行时最终混合在一起,而不是独立检查每个 &lt;data&gt; 元素.这意味着数据 URI 可以将任何方案与具有任何路径前缀的任何主机匹配,这不是所需的。

    解决方案是有多个intent-filter 部分,如下所示:

    <intent-filter>
      <action android:name="android.intent.action.VIEW" />
      <action android:name="android.intent.action.SENDTO" />
      <category android:name="android.intent.category.DEFAULT" />
      <category android:name="android.intent.category.BROWSABLE" />
      <data android:scheme="http" android:host="www.satur9nine.com" android:pathPrefix="/app" />
    </intent-filter>
    <intent-filter>
      <action android:name="android.intent.action.VIEW" />
      <action android:name="android.intent.action.SENDTO" />
      <category android:name="android.intent.category.DEFAULT" />
      <category android:name="android.intent.category.BROWSABLE" />
      <data android:scheme="satur9nine" android:host="*" />
    </intent-filter>
    

    intent-filter 匹配将以这种方式运行两次,并且不会混合方案、主机和路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多