Android intent-filter to be notified of an attempt to view a CSV file in the Download Manager

风格不统一 提交于 2020-08-02 05:13:28

问题


What's the required magic incantation to register your app to be notified that a user is trying to view a CSV file they've previously downloaded via the Download manager. I've had a brief play and the following filter will happily intercept notifications generated by: Dropbox, Box, Google Drive, Sky Drive, Chrome..... and offer itself to open the files, but my App isn't getting anything when a user attempts to view a file via the Download Manager.

        <intent-filter>
            <action android:name="com.my.testImportApp.LAUNCH" />
            <action android:name="android.intent.action.SEND" />
            <action android:name="android.intent.action.VIEW" />
            <action android:name="android.intent.action.EDIT" />
            <action android:name="android.intent.action.PASTE" />
            <action android:name="android.intent.action.OPEN_DOCUMENT" />
            <action android:name="android.intent.action.GET_CONTENT" />
            <action android:name="android.intent.action.INSERT" />
            <action android:name="android.intent.action.INSERT_OR_EDIT" />
            <action android:name="android.intent.action.SENDTO" />
            <action android:name="android.intent.action.SEND_MULTIPLE" />
            <action android:name="android.intent.action.PICK" />
            <category android:name="android.intent.category.CATEGORY_BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
            <data android:scheme="http" />
            <data android:scheme="https" />
            <data android:scheme="ftp" />
            <data android:scheme="file" />
            <data android:scheme="data" />
            <data android:scheme="info" />
            <data android:scheme="data" />
            <data android:scheme="smb" />
            <data android:scheme="nfs" />
            <data android:scheme="android.resource" />
            <data android:mimeType="text/comma-separated-values"/>
            <data android:mimeType="text/csv"/>
            <data android:mimeType="text/plain"/>
        </intent-filter>

回答1:


After a bit of playing, the following appears to work:

        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <action android:name="android.intent.action.SEND_MULTIPLE" />
            <action android:name="android.intent.action.VIEW" />
            <action android:name="android.intent.action.OPEN_DOCUMENT" />
            <action android:name="android.intent.action.PICK" />
            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.ALTERNATIVE" />
            <data android:scheme="http" />
            <data android:scheme="https" />
            <data android:scheme="ftp" />
            <data android:scheme="file" />
            <data android:scheme="data" />
            <data android:scheme="info" />
            <data android:scheme="data" />
            <data android:scheme="smb" />
            <data android:scheme="nfs" />
            <data android:mimeType="text/comma-separated-values"/>
            <data android:mimeType="text/csv"/>
            <data android:mimeType="text/plain"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <action android:name="android.intent.action.SEND_MULTIPLE" />
            <action android:name="android.intent.action.VIEW" />
            <action android:name="android.intent.action.OPEN_DOCUMENT" />
            <action android:name="android.intent.action.PICK" />
            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.ALTERNATIVE" />
            <data android:scheme="http" />
            <data android:scheme="https" />
            <data android:scheme="ftp" />
            <data android:scheme="file" />
            <data android:scheme="data" />
            <data android:scheme="info" />
            <data android:scheme="data" />
            <data android:scheme="smb" />
            <data android:scheme="nfs" />
            <data android:host="*" />
            <data android:pathPattern="/.*\\.csv" />
            <data android:pathPattern="/.*\\.CSV" />
            <data android:pathPattern="/.*\\.txt" />
            <data android:pathPattern="/.*\\.text" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <action android:name="android.intent.action.SEND_MULTIPLE" />
            <action android:name="android.intent.action.VIEW" />
            <action android:name="android.intent.action.OPEN_DOCUMENT" />
            <action android:name="android.intent.action.PICK" />
            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.ALTERNATIVE" />
            <data android:mimeType="text/comma-separated-values"/>
            <data android:mimeType="text/csv"/>
            <data android:mimeType="text/plain"/>
        </intent-filter>



回答2:


You don't need really to play, here is a quick roundtrip to find out what you need.

  1. Grep logcat for "ActivityManager: START u0"
  2. Place your file in Downloads
  3. Open Download App
  4. Open your file by click on it
  5. Check logcat you should find a line like: ```{act=android.intent.action.VIEW dat=content://com.android.providers.downloads.documents/document/2183 typ=text/csv flg=0x3 ...´´´

This means your scheme is "content" and your mime type is "text/csv". As a hint when you see the dialog "open with" leave it open and do a ```./gradlew installDebug´´´ with your new filter settings and it will immediately appear when you hit the filter conditions in your manifest set.

| improve this answer | |

来源:https://stackoverflow.com/questions/32096705/android-intent-filter-to-be-notified-of-an-attempt-to-view-a-csv-file-in-the-dow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!