Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 333
Alfie Woodland Avatar asked Apr 09 '15 11:04

Alfie Woodland


People also ask

What is intent filter in Android?

Android Intent Filters with Examples In android, Intent Filter is an expression in the app’s manifest file (ActivityMainfest.xml) and it is used to specify the type of intents that the component would like to receive.

What are intent services in Xamarin Android?

Intent Services in Xamarin.Android. Intent Services Overview. Both started and bound services run on the main thread, which means that to keep performance smooth, a service needs to perform the work asynchronously.

How do I know if multiple intent filters are compatible?

If multiple intent filters are compatible, the system displays a dialog so the user can pick which app to use. An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to receive.

What are the system requirements for app linking in Xamarin?

This guide requires Xamarin.Android 6.1 and an application that targets Android 6.0 (API level 23) or higher. App-linking is possible in earlier versions of Android by using the Rivets NuGet package from the Xamarin Component store. The Rivets package is not compatible with app-linking in Android 6.0; it does not support Android 6.0 app linking.


2 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>
like image 156
moljac Avatar answered Oct 25 '22 15:10

moljac


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>
like image 31
Ryan O Avatar answered Oct 25 '22 16:10

Ryan O