Alfie Woodland Alfie Woodland - 1 month ago 11
Android Question

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 ?

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>