开发者

Correct Android intent-filter configuration to associate a file type with an Activity?

开发者 https://www.devze.com 2023-01-24 16:59 出处:网络
This question has been asked [numerous times] before, but I have not seen any definitive answers, or examples of code that actually works.

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?


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>


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>


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号