2

我正在尝试制定过滤某些特定网址的意图。我试图捕捉的网址是:

这可以通过

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

我的问题来了,因为我已经有一个网址:

而且我不想尝试在该网址中打开应用程序。

我已经试过了

  • android:pathPattern="[app]
  • android:pathPattern="[app.*]
  • android:pathPattern="[app?.*]
  • android:pathPattern="[app\?.*]
  • android:pathPattern="[app\\?.*]
  • android:pathPattern="[app|app?.*]
  • android:pathPattern="[app|app\?.*]
  • android:pathPattern="[app|app\\?.*]
  • android:pathPattern="[nativeapp\z|nativeapp\?.*|nativeapp/.*]"
  • android:pathPattern="[nativeapp\\z|nativeapp\\?.*|nativeapp/.*]"

没有一个工作。甚至[app\\?.*]打开了/appinstall。

注意:在有人问之前。我有 /appinstall 控制器,因为我正在开发的应用程序和 iPhone 应用程序和 appInstall url 有很多情况可以处理重定向到应用程序商店。

4

2 回答 2

2

您需要使用android:path而不是android:pathPrefix或,android:pathPattern因为这将/app完全匹配路径并且/appinstall将被忽略。

<!-- Matches "http://host.com/app" exactly, note that querystring, fragment
    identifiers and the trailing slash are not considered part of the path and 
    are therefore ignored so URLs that will match:
       http://host.com/app
       http://host.com/app/
       http://host.com/app?some=value
       http://host.com/app/?some=value
       http://host.com/app#fragmentIdentifier
       http://host.com/app/#fragmentIdentifier
       http://host.com/app?some=value#fragmentIdentifier
       http://host.com/app/?some=value#fragmentIdentifier
    URLs that will NOT match
       http://host.com/app/index.htm
       http://host.com/appinstall
       http://host.com/appinstall/
       http://host.com/app/subdirectory
       http://host.com/app/subdirectory/
       http://host.com/apple.htm
 -->
<data android:scheme="http" android:host="host.com" android:path="/app" />

如果您还想匹配网站的根目录,则需要添加一个附加<data>元素:

<data android:scheme="http" android:host="host.com" android:path="/" />
于 2012-08-21T08:30:09.643 回答
0

android:pathPattern 非常初级,并不是真正的正则表达式,它只允许运算符“。” 和 ”*”。

你必须非常聪明才能实现你的目标。一种方法可能是...

<!-- Matches "http://host.com/app" exactly, URLs that will match:
       http://host.com/app
       http://host.com/app/
 -->
<data android:scheme="http" android:host="host.com" android:path="/" />

<!-- Matches URLs prefixed with "http://host.com/app/"
       http://host.com/app/
       http://host.com/app/?query=value
       http://host.com/app/somepage.htm
 -->
<data android:scheme="http" android:host="host.com" android:pathPrefix="/app/" />

<!-- Matches URLs prefixed with "http://host.com/app?"
       http://host.com/app?query=value
 -->
<data android:scheme="http" android:host="host.com" android:pathPrefix="/app?" />

编辑:

请忽略上述内容,我发现这是不正确的,因为您能够匹配的“路径”不包括查询字符串或片段标识符部分或 URL 的尾部斜杠。我会在一秒钟内提供另一个答案。

于 2012-08-16T15:54:52.573 回答