【问题标题】:Intent filter to respond to all URLs in Android?意图过滤器响应Android中的所有URL?
【发布时间】:2016-09-29 07:22:08
【问题描述】:

我想制作一个涉及响应设备上所有 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="www.youtube.com" android:scheme="http" />
</intent-filter>

但是如何定义一个意图过滤器来处理任何不只是属于特定域或主机的 URL?可能吗?

【问题讨论】:

    标签: android url android-intent intentfilter


    【解决方案1】:

    定义架构如:

    <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:scheme="http"/>
        <data android:scheme="https"/>
        <data android:scheme="about"/>
        <data android:scheme="javascript"/>
    </intent-filter>
    

    【讨论】:

      【解决方案2】:

      只需从您发布的代码中删除以下行:

        <data android:host="www.youtube.com" android:scheme="http" />
      

      所以你最终会得到

      <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
      </intent-filter>
      

      或者,您可能只想将行缩短为

        <data android:scheme="http" />
      

      这将使您的应用仅拦截 http URL,因此基本上是链接。

      【讨论】:

        【解决方案3】:

        尽管官方 android 教程说您可以简单地删除数据过滤器以减少您的意图过滤器的限制,但这样做可能会导致“缺少数据元素”错误。 您可以改为执行以下操作

        <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="*.com" android:scheme="http" />
        </intent-filter>
        

        您还可以为 .net、.org 等添加数据过滤器

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-05-14
          • 1970-01-01
          • 1970-01-01
          • 2012-02-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-26
          相关资源
          最近更新 更多