Intent Filters and android:pathPattern

2019-04-28 19:58发布

问题:

In my application, I want to handle links that use the following pattern:

scheme://host/folder1/folder2/folder3/folder4/article

I got it to work temporarily, by using the following:

<intent-filter>
    <data android:scheme="http" />
    <data android:host="www.laprensa.com.ni" />
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
</intent-filter>

However, as you may imagine that opens any link that starts with scheme://host, and I want to make sure I only pick up on those that have the above stated pattern, where the page is 4 folders inside the host.

Another little problem is that the folder names are never the same, and therefore I cannot simply use android:path. It's also worth noting that the android:pathPrefix is not the same, as the first three folder are date related.

For instance, the urls are strucutred something like this:

scheme://host/year/month/day/articleType/article

I've been reading the docs and questions on how to use android:pathPattern but I really don't understand what I'm supposed to type in.

Any ideas?

Thanks!

EDIT

Upon suggestion, I tried:

android:pathPattern="/.*/.*/.*/.*/.*"

Where each '.*' represented a folder but that seems to still pick up the other URLs that are not articles. For example, here are two different urls:

Url I want to handle:

http://www.laprensa.com.ni/2013/05/10/vida/145996-lionel-richie-defiende-a

Url I don't want to handle

http://www.laprensa.com.ni/2013/05/10/vida/

I guess the problem here is that they both have the same amount of levels, the only difference is that one actually has something after that last '/', any other ideas I can try? I did try adding one more '/.*' but that stopped working completely and the app stopped handling any links period :(

回答1:

You're close, but missing the final part. This should work for you:

android:pathPattern="/.*/.*/.*/.*/..*"

This will match any paths with four repeating /[anything] as well as enforce that you must have something after the final /.

Just as you asked:

Url I want to handle:

http://www.laprensa.com.ni/2013/05/10/vida/145996-lionel-richie-defiende-a

Url I don't want to handle

http://www.laprensa.com.ni/2013/05/10/vida/

Explanation

The . enforces a pattern of "one of any character". The .* enforces "any number (or none!) of any character".

Combining these two, ..* enforces "any number of any characters, but at least one must be provided"



回答2:

You should add exactly the android:pathPattern attribute to configuration you defined. As reported in Android documentation, android:pathPattern can contain following wildcards for building simplified regular expressions:

  • An asterisk ('*') matches a sequence of 0 to many occurrences of the immediately preceding character.
  • A period followed by an asterisk (".*") matches any sequence of 0 to many characters.

In your case:

<data android:scheme="http"
      android:host="www.laprensa.com.ni"
      android:pathPattern="/.*/.*/.*/.*/.*" />

Where each .* represent a folder name.