Intent Filters and android:pathPattern

24,878

Solution 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"

Solution 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.

Share:
24,878
daniel_c05
Author by

daniel_c05

Updated on May 14, 2020

Comments

  • daniel_c05
    daniel_c05 about 4 years

    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 :(

  • daniel_c05
    daniel_c05 about 11 years
    Somehow I thought it worked, but it didn't, I updated the question.
  • thetonrifles
    thetonrifles about 11 years
    Unfortunately, wildcards used for android:pathPattern are a very reduced subset of common regular expressions. Basing on documentation it's not possible to handle your need only by using android:pathPattern. Considering you're trying to access documents, you can try to exploit the additional attribute android:mimeType. There you can use value text/* or for example text/html.
  • Shubham AgaRwal
    Shubham AgaRwal over 3 years
    @thetonrifles can you please review stackoverflow.com/questions/65809067/…
  • Shubham AgaRwal
    Shubham AgaRwal over 3 years
  • Jonas Borggren
    Jonas Borggren almost 3 years
    If i was looking at an url like https://www.stackoverflow.com/users/exampleuser-225/0123456 and the id I wanted to fetch was 0123456, would it then be possible to use android:pathPattern="/users/..*/..*" ?