打开我的特定文件和URL扩展的应用程序 - intent-filter未按预期工作

时间:2013-03-25 22:22:17

标签: android android-intent mime-types intentfilter

问题:

如何让我的应用程序无条件地打开文件和网址扩展名?

我在设置我的intent-filter时已经结束了,因为它没有任何意义。我的最终目标是打开在某个扩展名中以path结尾的任何内容。举个例子,让我们选择“。riley”作为我的目标延伸。

我的基地`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="*"
        android:mimeType="*/*"
        android:pathPattern=".*\\.riley" />
</intent-filter>

目前可以从filecontent打开。

问题#1:pathPattern

对于初学者来说,好像data参数pathPattern没有做任何事情,正如我所设定的那样:

<data
    android:host="*"
    android:mimeType="*/*"
    android:pathPattern=".*\\.riley" />

..但我仍然可以打开Intent Urifile路径,这些路径不会以content和{{1}下的“。riley”结尾方案。

我的应用程序使用此intent-filter打开的几个示例路径是:

Path: /storage/sdcard0/Download/MyApp.apk Path: /messagesByAccount/1

为了清晰起见,“路径:”只是我在日志中添加的内容。第一个来自file方案下的gmail,第二个来自content方案下的gtalk。鉴于此,我已经对intent-filters如何工作感到困惑。

pathPatternfile计划忽略了content

问题#2:在浏览器中打开

我试图让我的应用程序从浏览器打开“。riley”扩展程序,但重定向链接没有MIME类型,因此我删除了mimeType值来自data模式(之前我有android:mimeType="*/*",因为MIME类型不一致)并且允许我从重定向URL打开就好了。

当我尝试从浏览器打开直接的“。riley” URL(例如:http://thisisnotarealdomainname.com/myfile.riley)并且从我的应用程序打开的选项消失时,出现了问题。将mimeType值添加回data模式允许我从直接网址开启。

intent-filter不允许我打开直接和间接网址。

问题#3:方案

我希望能够从多种不同的方案类型中打开(例如:http,文件,内容), but as soon as I remove方案altogether, it disqualifies my application from opening the http scheme and when I specify scheme =“*”, it disqualifies my application from opening ANY scheme. I've also tried adding more数据模式,但我感觉好像只有一个生效,因为这对我没有任何帮助。

因此,指定http将允许http,但显然不允许filecontent并且未指定scheme禁用{{} 1}}。

我无法从所有不同的方案中打开。

重申一下, 如果给出这些复杂性,我怎样才能让我的应用程序无条件地打开文件和网址扩展

1 个答案:

答案 0 :(得分:3)

问题#1:<data android:pathPattern=".*\\.riley" />无效。引用the documentation

  仅当为过滤器指定了方案和主机属性时,

[pathPattern]才有意义。

问题#2:&#34;但重定向链接不具有MIME类型&#34;应该是不正确的,除非Web服务器严重破坏。 HTTP重定向本身没有MIME类型,但应该由浏览器本身处理,因为在浏览器处理重定向之前,它不知道URL是否指向它应该处理的东西(例如,网页)或者应该下载的东西。重定向的结果 - HTTP 200 OK响应 - 应该具有MIME类型。如果您确信重定向到下载的URL不起作用,请创建一个演示错误的示例项目并将其发布到某处以供审阅。

问题#3:&#34;我不能从所有不同的方案开放。&#34; - 在单独的<data>元素中使用多个android:scheme元素和多个<intent-filter>属性。

例如,AOSP音乐应用程序具有:

    <activity android:name="AudioPreview" android:theme="@android:style/Theme.Dialog"
            android:taskAffinity=""
            android:excludeFromRecents="true" android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="file"/>
            <data android:mimeType="audio/*"/>
            <data android:mimeType="application/ogg"/>
            <data android:mimeType="application/x-ogg"/>
            <data android:mimeType="application/itunes"/>
        </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="audio/*"/>
            <data android:mimeType="application/ogg"/>
            <data android:mimeType="application/x-ogg"/>
            <data android:mimeType="application/itunes"/>
        </intent-filter>
        <intent-filter
            android:priority="-1">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="content" />
            <data android:mimeType="audio/*"/>
            <data android:mimeType="application/ogg"/>
            <data android:mimeType="application/x-ogg"/>
            <data android:mimeType="application/itunes"/>
        </intent-filter>
    </activity>

您会注意到所有三个<intent-filter>节中的操作,类别和MIME类型都相同;只有android:scheme属性不同。

现在,此音乐应用示例的file方案仅在某些本地文件管理器将适当的MIME类型附加到Intent时才有效。音乐不会尝试使用android:pathPattern来匹配文件扩展名。事实上,Google很少使用android:pathPattern。 AOSP日历应用程序中有一次出现:

        <intent-filter
           android:priority="50">
           <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="www.google.com" android:pathPrefix="/calendar/event" />
           <data android:scheme="https" android:host="www.google.com" android:pathPrefix="/calendar/event" />
           <data android:scheme="http" android:host="www.google.com" android:pathPattern="/calendar/hosted/.*/event" />
           <data android:scheme="https" android:host="www.google.com" android:pathPattern="/calendar/hosted/.*/event" />
        </intent-filter>

但这几乎就是这样。当然其他人已经尝试过了 - StackOveflow上有很多关于其使用的问题。

相关问题