在 Xamarin Mono 中使用 Intent 过滤器处理特定 URL for Android

Handling specific URLs with intent filters in Xamarin Mono for Android

我正在尝试让我的 Activity 处理 url 的形式 mydomain.comwww.mydomain.com 以及 http 和 [=16] =] 计划。目前,我的 activity 的 IntentFilter 属性如下所示:

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

在清单中生成此文件,但似乎不适用于任何必需的 url 配置:

<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>

如何更改此属性,以便我的 activity 能够处理所有格式为 http(s)://(www.)[=25 的 url =] ?

您可以添加多个 Intent 过滤器属性

[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 {}

结果 xml 将是

<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>

压缩单个 IntentFilter:

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

将生成以下 AndroidManifest.xml 节点:

  <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>