Alfie Woodland
Alfie Woodland

Reputation: 834

Handling specific URLs with intent filters in Xamarin Mono for Android

I'm attempting to have my Activity handle urls taking the form of mydomain.com or www.mydomain.com with both http and https schemes. Currently the IntentFilter attribute for my activity looks like this:

[IntentFilter(
  new[] { Intent.ActionView },
  Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
  DataHost = "mydomain.com",
  DataScheme = "http"
)]

Which generates this in the manifest, and doesn't appear to be working for any of the required url configurations:

<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:host="mydomain.com" android:scheme="http" />
</intent-filter>

How can I change this attribute so that my activity will handle all urls of the form http(s)://(www.)mydomain.com ?

Upvotes: 10

Views: 4740

Answers (2)

moljac
moljac

Reputation: 1010

Compressed single IntentFilter:

[
    IntentFilter
    (
        new[] { Intent.ActionView },
        Categories = new[]
            {
                Intent.CategoryDefault,
                Intent.CategoryBrowsable
            },
        DataSchemes = new[] { "http", "https" },
        DataHosts = new[] { "*.xamarin.com", "xamarin.com" },
        DataMimeType = "text/plain"
    )
]

will generate following AndroidManifest.xml node:

  <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:mimeType="text/plain" />
    <data android:host="*.xamarin.com" />
    <data android:host="xamarin.com" />
    <data android:scheme="http" />
    <data android:scheme="https" />
  </intent-filter>

Upvotes: 8

Ryan O
Ryan O

Reputation: 326

You can add multiple intent filter attributes

[IntentFilter(
  new[] { Intent.ActionView },
  Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
  DataHost = "mydomain.com",
  DataScheme = "http"
)]
[IntentFilter(
  new[] { Intent.ActionView },
  Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
  DataHost = "mydomain.com",
  DataScheme = "https"
)]
[IntentFilter(
  new[] { Intent.ActionView },
  Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
  DataHost = "*.mydomain.com",
  DataScheme = "http"
)]
[IntentFilter(
  new[] { Intent.ActionView },
  Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
  DataHost = "*.mydomain.com",
  DataScheme = "https"
)]
public class MyActivity : Activity {}

the resulting xml will be

<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:host="mydomain.com" android:scheme="http" />
</intent-filter>
<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:host="mydomain.com" android:scheme="https" />
</intent-filter>
<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:host="*.mydomain.com" android:scheme="http" />
</intent-filter>
<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:host="*.mydomain.com" android:scheme="https" />
</intent-filter>

Upvotes: 4

Related Questions