【问题标题】:Handling specific URLs with intent filters in Xamarin Mono for Android在 Xamarin Mono for Android 中使用意图过滤器处理特定 URL
【发布时间】:2015-04-09 11:15:24
【问题描述】:

我正在尝试使用httphttps 方案使我的活动句柄网址采用mydomain.comwww.mydomain.com 的形式。目前,我的活动的 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 形式的所有 url?

【问题讨论】:

    标签: android xamarin xamarin.android android-manifest intentfilter


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      [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 属性实例才能为这两个方案附加“www”别名?
      • 您可以对子域使用通配符,而无需添加额外的意图过滤器。
      • 如果您将通配符用法的示例附加到您的答案中,那就太好了。
      • 通配符*.mydomain.com 不涵盖第二层域本身,即mydomain.com,我理解了吗?
      • 这个answer 比重复属性要好得多。
      猜你喜欢
      • 2013-08-28
      • 1970-01-01
      • 2011-02-09
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多