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



文档指出通配符可以在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部分中有额外点的链接。

我是遗漏了什么,还是安卓系统的错误(无论是在代码中,还是在文档中)?

附言:这个过滤器捕捉所有这些链接:

<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>
是的,你说得对。是.造成了问题。

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

一种解决方案是添加多个模式,如下所示。添加的数量越多,它就可以有越多的.而不会出现问题。

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

另一个解决方案是使用此库。

Android为此使用PatternMatcher。而.符号在PatternMatcher中具有特殊的意义。因此,您必须使用转义符

试试这个:

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

此外,由于在从XML读取字符串时用作转义符(在将其解析为模式之前)。所以,你可能需要双重逃离它。

像这样:

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

最新更新