Jonas Van der Aa
Jonas Van der Aa

Reputation: 1461

Matching a URL to an Intent not working

I'm writing an Android app that accompanies a website and I wanted to make the app intercept certain URLs. I found multiple examples through Google and on SO but none of them work. I create my Intent:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://xgn.nl/article/" + String.valueOf(id)));

and then launch it from another Activity (where i is the intent from above)

startActivity(i);

The intent filter for the activity that should receive this is:

<activity android:name=".ArticleActivity" android:theme="@style/XGN.Red">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="http" android:host="xgn.nl" android:pathPrefix="/article/" android:mimeType="text/*" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
        </intent-filter>
    </activity>

I also tried it without the mimeType in the filter. The intent resolver sees the filter but complains that the data does not match with the data in the intent and launches the browser instead.

What am I doing wrong?

Upvotes: 2

Views: 642

Answers (1)

Jonas Van der Aa
Jonas Van der Aa

Reputation: 1461

I actually solved it 5s after I posted it, the problem was that you need to specify the fully qualified class name for this to work :)

<activity android:name="nl.xgn.ArticleActivity" android:theme="@style/XGN.Red">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="http" android:host="xgn.nl" android:pathPrefix="/article/" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
    </intent-filter>
</activity>

EDIT: To be more precise, the error I was seeing in logcat was wrong. The data type did match, but it chose the more visible browser class over my lazily defined activity. Specifying the full class name and removing the mimeType made this work.

Upvotes: 4

Related Questions