【问题标题】:How to open my Android app when rss link is opened in browser?在浏览器中打开 rss 链接时如何打开我的 Android 应用程序?
【发布时间】:2011-02-04 18:37:00
【问题描述】:

我正在为我的 Android 手机创建一个 RSS 聚合器。我希望能够从浏览器订阅 rss 提要,因为大多数网站都有通过 rss 订阅的按钮。

如何构建意图过滤器来接收这些链接?

这个问题类似,展示了如何创建一个意图过滤器来处理浏览器链接: Make a link in the Android browser start up my app?

但是,我不知道如何使其特定于 rss 提要。 作为尝试,我尝试了这个过滤器:

        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <data android:mimeType="application/rss+xml" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" /> 
        </intent-filter>

我在正确的轨道上吗?我应该过滤什么?

【问题讨论】:

标签: android rss android-intent intentfilter


【解决方案1】:

这 3 个过滤器似乎是 GoogleListen 和 GoogleReader 应用使用的过滤器:

<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:host="*"/>
    <data android:pathPattern=".*\\.xml"/>
    <data android:pathPattern=".*\\.rss"/>
</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:scheme="http"/>
    <data android:host="feeds.feedburner.com"/>
    <data android:host="feedproxy.google.com"/>
    <data android:host="feeds2.feedburner.com"/>
    <data android:host="feedsproxy.google.com"/>
</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:scheme="http"/>
    <data android:mimeType="text/xml"/>
    <data android:mimeType="application/rss+xml"/>
    <data android:mimeType="application/atom+xml"/>
    <data android:mimeType="application/xml"/>
</intent-filter>

【讨论】:

  • 这节省了我一个小时的尝试时间。如果可能的话会给+10。只是出于好奇:您是如何发现 Google Listen 和 Google Reader 使用了哪些过滤器的?
【解决方案2】:

事实证明,可以通过多种不同的方式设置播客,因此每个意图过滤器仅适用于其中的一些。需要使用许多不同的过滤器来获得对大多数订阅链接的预期效果。

以下是我发现的一些有效的过滤器:

<intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="itpc" />
                <data android:scheme="pcast" />
                <data android:scheme="feed" />
                <data android:scheme="rss" />
            </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:scheme="http" android:host="*"
                    android:pathPattern=".*xml" />
                <data android:scheme="http" android:host="*"
                    android:pathPattern=".*rss" />
                <data android:scheme="http" android:host="*"
                    android:pathPattern=".*feed.*" />
                <data android:scheme="http" android:host="*"
                    android:pathPattern=".*podcast.*" />
                <data android:scheme="http" android:host="*"
                    android:pathPattern=".*Podcast.*" />
                <data android:scheme="http" android:host="*"
                    android:pathPattern=".*rss.*" />
                <data android:scheme="http" android:host="*"
                    android:pathPattern=".*RSS.*" />
            </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:mimeType="text/xml" android:scheme="http" />
                <data android:mimeType="application/rss+xml" android:scheme="http" />
                <data android:mimeType="application/atom+xml" android:scheme="http" />
            </intent-filter>

【讨论】:

  • 为什么通过intent过滤器获取到网站的url后app会被关闭?我的意思是在我的应用程序中,当我使用意图过滤器添加获取网站 url 时,它会显示在 textview 中,但会关闭并返回到我点击链接的浏览器应用程序
【解决方案3】:

我走对了吗?

是的。然而:

  • 您不需要该方案
  • 无论如何,您在scheme 前面缺少android: 前缀
  • type 应该是 android:mimeType

Here is a sample application 演示响应 PDF 文件上的链接等内容。

【讨论】:

  • 我进行了更正,但我仍然无法让我的应用响应链接
【解决方案4】:

可能是服务器没有发送正确的 mimetype 吗?

添加实验:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.BROWSABLE" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="http" />
    <data android:host="*" />
    <data android:pathPattern=".*\\.rss" />
    <data android:pathPattern=".*\\.xml" />
  </intent-filter>

编辑: 组合正确的意图过滤器集有点棘手,匹配算法中有很多早期返回:
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/content/IntentFilter.java

在您的情况下,mimetype 必须与“application/rss+xml”完全匹配,您的源是否在消息头中返回了该 mimetype?

  URL url = new URL("http://www.engadget.com/rss.xml");
  URLConnection conn = url.openConnection();
  System.out.println("Content-Type:"+conn.getHeaderField("Content-Type"));

尝试:

  1. 制作更广泛的过滤器
  2. 添加多个过滤器。

【讨论】:

  • 那也行不通。此外,现在大多数提要不使用 .rss 或 .xml 扩展名,它们只是使用一些默认 url,有时甚至是 php 页面。
  • 他们使用mime类型,将数据行替换为:
猜你喜欢
  • 1970-01-01
  • 2013-07-04
  • 1970-01-01
  • 2019-04-05
  • 2010-12-09
  • 1970-01-01
  • 1970-01-01
  • 2014-01-07
  • 1970-01-01
相关资源
最近更新 更多