奇怪的Intent过滤器路径模式行为

时间:2018-08-18 20:07:27

标签: android android-intent intentfilter

Documentation states表示可以在pathPattern中使用通配符。

  

一个句点后跟一个星号(。*)可以匹配任何0到许多字符的序列。

因此,我创建了以下过滤器:

<intent-filter android:priority="600">
    <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="*"/>
    <data android:pathPattern="/.*.exe"/>
</intent-filter>

但是,对于所有以“ .exe”结尾的链接来说,它并不“起作用”。

可使用以下链接:

https://subdomain.site.org/lite/appinst-lite-vc.exe

https://subdomain.site.org/appinst.exe

不适用于此链接:

https://subdomain.freedownloadmanager.org/5/5.1-latest/app_x86_setup.exe

看来,path部分中带有多余点的链接不起作用。

我是否缺少某些东西?或者是Android错误(在代码或文档中)?

P.S。该过滤器捕获所有这些链接:

<intent-filter android:priority="600">
    <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="*"/>
    <data android:pathPattern="/.*"/>
</intent-filter>

2 个答案:

答案 0 :(得分:1)

Android为此使用PatternMatcher.符号在PatternMatcher中具有特殊含义。因此,您必须使用转义字符\

尝试一下:

<data android:pathPattern="/.*\.exe"/>

此外,由于从XML读取字符串时(在将其解析为模式之前),\用作转义字符。因此,您可能需要对其进行两次转义。

赞:

<data android:pathPattern="/.*\\.exe"/>

答案 1 :(得分:1)

是的,您是对的。是造成问题的aggregate()

与正则表达式不同,在路径模式中,匹配将在模式的第一个字符的第一个匹配处停止。换句话说,*匹配是非贪婪的。

一种解决方案是添加多个模式,如下所示。添加的数量越多,它可以有更多或.个数字而没有问题。

.

另一种解决方案是使用this库。