Android 的 intent-filter 匹配文件而不考虑其扩展名

标签 android android-intent intentfilter

我希望我的应用程序能够打开包含 XML 内容的 .src 文件。它部分起作用;有一个问题我无法解决:看起来 Android 在匹配 Intent 时没有考虑 android:pathPattern 。我的 intent-filter 看起来像这样:

<intent-filter>
    <action android:name="com.google.android.apps.drive.DRIVE_OPEN" />
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:host="*" android:mimeType="text/xml" android:pathPattern=".*\\.src" />
    <!-- path pattern does not match dots correctly: http://stackoverflow.com/q/3400072/44089 -->
    <data android:pathPattern=".*\\..*\\..*\\.src" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\.src" />
    <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.src" />
</intent-filter>

在我添加 android:mimeType="text/xml" 之前,实际上每个“查看”操作都与我的应用程序匹配,即我的应用程序在应用程序选择器中可见,即使我想查看联系人。幸运的是,添加 mimeType 过滤有点帮助。但是,pathPattern 似乎并没有改变我的 Intent 过滤的工作方式。

我看过几十篇关于这个的教程,代码都是一样的。这一定是一些奇怪的、不寻常的案例。我究竟做错了什么?如果有任何想法或建议,我将不胜感激。

最佳答案

引用 the documentation for android:path* attributes :

These attributes are meaningful only if the scheme and host attributes are also specified for the filter.

你似乎没有计划。

关于Android 的 intent-filter 匹配文件而不考虑其扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26314927/

相关文章:

android - Android 中的 HttpPost - 无法识别的字符

java - 访问 DownloadProvider 时避免 SecurityException

java - 需要将字符串转换为我可以发送的 Intent 吗?

android - 如何设置自定义zip文件的mime类型?

android - 使用 Intent 过滤器收听YouTube链接

android - (Android Studio)快速完成外部类的建议变量名?

java - 如果屏幕未旋转且其他 ImageView 已更新,则 ImageView 不会显示图像

android - 获取Android通知面板宽度

java - 父 Activity 的 Android 实例

java - 使用 Intent 启动 Activity 时遇到问题