android – Matching a url pattern in <intent-filter>-ThrowExceptions

Exception or error:

I’d like one of my activities to pick up a particular url. The pattern is:

http://www.example.com/abc123/foo/xyz789

The path components “abc123” and “xyz789” can be any sequence of alpha-numerics, length > 1.

Doing this in my manifest:

<activity>
  <intent-filter>
    <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"
      android:host="example.com" 
      android:pathPattern="/.*/foo/.*" />

but it seems that any pattern from my domain is getting matched, ie:

myexample.com
myexample.com/whatever

both get matched. I guess maybe the .* operator is not working as I expect here? Any help would be great,

Thanks

http://developer.android.com/guide/topics/manifest/data-element.html

How to solve:

I had the same problem today, my solution was:

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

using (..*) as a replacement for (.+)

In the original question’s case I guess that this can work:

<data
                android:host="example.com"
                android:pathPattern="..*/foo/..*"
                android:scheme="http"/>

###

Since android OS uses PatternMatcher and observing from source code and documentation it can only work with . * and \\(escape characters) we might need to be a little creative – i.e., use \\ to stop regex evaluation and then resume from then onwards.

<data
                android:host="example.com"
                android:pathPattern=".*\\/foo/.*"
                android:scheme="http"/>

which will only match myexample.com/whatever and not myexample.com

To create different intent filters with varying uri use / at the end of the uri like

Sample solution, use http://www.example.com/abc123/foo/xyz789/ instead of http://www.example.com/abc123/foo/xyz789 and then use

example.com/abc123/ android:pathPattern=".*\\/

example.com/abc123/foo/xyz789/ android:pathPattern=".*\\/foo/.*\\/

example.com/abc123/foo/xyz789/bar/uvx456/ android:pathPattern=".*\\/foo/.*\\/bar/.*\\/

###

the problem is that .* matches everything, -and nothing-, so “/.*/” matches “//”, which is equivalent to just “/” in a path.

also, you can ensure that the two path segments “abc123” in your example are equal with the expression matcher supported here.

in other words, you want want to alter your paths to something simpler that can be matched. if it’s your component that’s receiving the intent, you can pass a fake URL that is just the variable parts of the real URL. for example, if you want to match,

http://www.example.com/.+/foo/.+

where the two captured groups are equal, just pass http://mystuff/abc123 in the intent, and set the data pattern to http://mystuff/*

then parse that when you receive the intent, and transform it into the real URL by filling in the known constant parts.

Leave a Reply

Your email address will not be published. Required fields are marked *