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

10

我想让我的Activity处理以下形式的url:mydomain.com 或者 www.mydomain.com,包括httphttps协议。目前,我的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>

我该如何更改此属性,以便我的活动将处理所有形式为 http(s)://(www.)mydomain.com 的网址?

2个回答

8

压缩的单个 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>

4

您可以添加多个意图过滤器属性。

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

您可以使用通配符来处理子域名,而无需添加额外的意图过滤器。 - Ryan O
如果您在回答中附上通配符使用示例,那就太好了。 - Zverev Evgeniy
我理解得对吗,通配符*.mydomain.com不包括二级域名本身,即mydomain.com - Zverev Evgeniy
这个回答比重复属性好得多:https://dev59.com/3l0b5IYBdhLWcg3wJ-bb#42933883 - Jose M.
Android 11增加了可见性功能,需要在清单文件中添加<queries>元素。是否可以使用属性而不是直接在清单文件中定义来在代码中定义它? - iBobb
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接