Home » Android » Handling specific URLs with intent filters in Xamarin Mono for Android

Handling specific URLs with intent filters in Xamarin Mono for Android

Posted by: admin June 16, 2020 Leave a comment

Questions:

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 ?

How to&Answers:

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>

Answer:

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>