【问题标题】:Matching a url pattern in <intent-filter>匹配 <intent-filter> 中的 url 模式
【发布时间】:2011-01-21 18:11:02
【问题描述】:

我希望我的一项活动能够获取特定的网址。模式是:

http://www.example.com/abc123/foo/xyz789

路径组件“abc123”和“xyz789”可以是任意字母数字序列,长度> 1。

在我的清单中这样做:

<activity>
  <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"
      android:host="example.com" 
      android:pathPattern="/.*/foo/.*" />

但似乎我域中的任何模式都在匹配,即:

myexample.com
myexample.com/whatever

两者都匹配。我想也许 .* 运算符没有像我期望的那样工作?任何帮助都会很棒,

谢谢

http://developer.android.com/guide/topics/manifest/data-element.html

【问题讨论】:

    标签: android android-intent intentfilter


    【解决方案1】:

    我今天也遇到了同样的问题,我的解决方法是:

       <data android:pathPattern="/remotes/..*/..*"/>
    

    使用 (..*) 代替 (.+)

    在原始问题的情况下,我想这可以工作:

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

    【讨论】:

    • 这是超值的。 ..* 匹配任何字符的 1 次或多次出现。
    • 非常感谢!这正是我想要的! :)
    【解决方案2】:

    由于 android OS 使用 PatternMatcher 并从 source code 和文档中观察,它只能与 . *\\(转义字符)一起使用,我们可能需要一点创意 - 即,使用 @ 987654326@ 停止正则表达式评估,然后从那时起恢复。

    <data
                    android:host="example.com"
                    android:pathPattern=".*\\/foo/.*"
                    android:scheme="http"/>
    

    只会匹配myexample.com/whatever 而不是myexample.com

    要创建具有不同 uri 的不同意图过滤器,请在 uri 末尾使用 /

    示例解决方案,使用 http://www.example.com/abc123/foo/xyz789/ 代替 http://www.example.com/abc123/foo/xyz789 然后使用

    example.com/abc123/android:pathPattern=".*\\/

    example.com/abc123/foo/xyz789/android:pathPattern=".*\\/foo/.*\\/

    example.com/abc123/foo/xyz789/bar/uvx456/android:pathPattern=".*\\/foo/.*\\/bar/.*\\/

    【讨论】:

      【解决方案3】:

      问题是 .* 匹配所有内容,-and nothing-,所以“/.*/”匹配“//”,相当于路径中的“/”。

      此外,您可以确保示例中的两个路径段“abc123”与此处支持的表达式匹配器相等。

      换句话说,您想将路径更改为可以匹配的更简单的路径。如果接收意图的是您的组件,您可以传递一个虚假的 URL,它只是真实 URL 的可变部分。例如,如果你想匹配,

      http://www.example.com/.+/foo/.+
      

      在两个捕获的组相等的情况下,只需在intent中传递http://mystuff/abc123,并将数据模式设置为http://mystuff/*

      然后在收到意图时对其进行解析,并通过填写已知的常量部分将其转换为真实的 URL。

      【讨论】:

      • 您好,感谢您提供的信息,这两个组会有所不同(更新 Q 以反映)。我们可以使用 .+ 运算符吗?这似乎仍然匹配一切:pattern="/.+/foo/.+"
      猜你喜欢
      • 1970-01-01
      • 2013-10-06
      • 2011-11-03
      • 1970-01-01
      • 2012-06-18
      • 2019-03-29
      • 2013-10-07
      • 2021-08-24
      • 1970-01-01
      相关资源
      最近更新 更多