Correct Android intent-filter configuration to ass

2019-04-09 21:32发布

This question has been asked [numerous times] before, but I have not seen any definitive answers, or examples of code that actually works.

I would like to associate an Activity with a particular file type.

For discussion, assume that I want my Activity to be associated with PDFs.

Here is what I currently have. I have experimented with many different values and combinations of values in the intent-filter, but I have yet to get my Activity to start when a PDF is selected.

<activity name="com.mycompany.MyActivity">
    <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="file" />
        <data android:mimeType="application/pdf" />
        <data android:pathPattern="\\*\\.pdf" />
        <data android:host="*" />
    </intent-filter>
</activity>

Does anyone know how to actually make this work?

3条回答
叼着烟拽天下
2楼-- · 2019-04-09 21:42

Have you tried with that simple version :

<activity name="com.mycompany.MyActivity">
    <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="application/pdf" />
    </intent-filter>
</activity>
查看更多
趁早两清
3楼-- · 2019-04-09 21:43

Your pathPattern is definitively wrong and you are restricting it too much with the mimetype.

Try the following:

<activity name="com.mycompany.MyActivity">
  <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=".*\\.pdf" />
  </intent-filter>
  <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:mimeType="application/pdf" />
  </intent-filter>
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="file" />
    <data android:host="*" />
    <data android:pathPattern=".*\\.pdf" />
  </intent-filter>
</activity>
查看更多
男人必须洒脱
4楼-- · 2019-04-09 22:05

To open both local and remote pdf files I'd do:

           <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="application/pdf" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="file" android:host="*" android:pathPattern=".*\\.pdf" />
        </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=".*\\.pdf" />
            <data android:scheme="https" android:host="*" android:pathPattern=".*\\.pdf" />
        </intent-filter>
查看更多
登录 后发表回答