如何在浏览器中打开rss链接时打开我的Android应用程序?

问题描述:

我正在为我的Android手机创建一个rss聚合器。我希望能够从浏览器订阅rss订阅源,因为大多数网站都通过rss按钮订阅。如何在浏览器中打开rss链接时打开我的Android应用程序?

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

这个问题是相似的,并展示了如何创建一个意图过滤器来处理浏览器链接: 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> 

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

+1

只是提醒:不要假设路径可以作为正则表达式给出,只是因为你经常看到“。*”。语法非常有限。请参阅http://developer.android.com/guide/topics/manifest/data-element.html获取关于这方面的一些细节。 – xmjx 2011-08-04 07:49:28

结果有很多不同的方式可以设置播客,所以每个意图过滤器只适用于其中的一些。需要使用许多不同的过滤器才能获得大多数订阅链接的预期效果。

这里的一些过滤器,我发现工作:

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

为什么应用程序在获取url后关闭通过意图过滤器的网站?我的意思是在我的应用程序中,当我使用intent过滤器来添加获取网址时,它显示在textview中,但被关闭并返回到我点击链接的浏览器应用程序 – 2013-09-22 21:57:18

我在正确的轨道上吗?

是的。但是:

  • 你不需要的方案
  • 你缺少的scheme前面android:前缀,反正
  • type应该android:mimeType

Here is a sample application演示,除其他事项外,回复PDF文件上的链接。

+0

我作出了更正,但仍然无法让我的应用程序对链接做出回应 – CodeFusionMobile 2011-02-04 23:05:38

难道是服务器不发送正确的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. 添加多个过滤器。
+0

这也行不通。此外,现在大多数Feed不使用.rss或.xml扩展名,他们只使用一些默认url,有时甚至是一个php页面。 – CodeFusionMobile 2011-02-28 18:55:35

+0

他们正在使用MIME类型,将数据行替换为: 2011-02-28 21:09:14

这3个过滤器似乎是由GoogleListen和Google阅读器的应用程序所使用的那些:

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