How do I make my app appear in app chooser?

2019-06-27 11:37发布

问题:

I want to advertise that my app is capable of viewing pdf files so that it will appear in the app chooser when a pdf file is selected from the file manager.

Here is what my intent filters look like

<activity
    android:name=".MainActivity"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <data android:mimeType="application/pdf" />
    </intent-filter>
</activity>

Whenever I open a pdf from file manager it automatically selects another pdf app called Polaris Viewer.

I checked to make sure that Polaris is not the default app, under application settings. It says no defaults set.

Also, I downloaded an third party app called Intent Intercept. If I select a pdf file from file manager an app chooser appears showing Polaris and Intent Intercept. If I choose Intent Intercept it tells me that both Polaris and my app (Rollout PdfEditor) match the intent. Here is the output from Intent Interceptor:

ACTION: android.intent.action.VIEW

DATA: file:///storage/sdcard0/Download/download.pdf TYPE: application/pdf

FLAGS: FLAG_ACTIVITY_FORWARD_RESULT FLAG_ACTIVITY_PREVIOUS_IS_TOP

EXTRAS: EXTRA 1: Class: java.lang.Boolean Key: preview Value: false EXTRA 2: Class: java.lang.String Key: key_filename Value: /storage/sdcard0/Download/download.pdf EXTRA 3: Class: android.net.Uri$HierarchicalUri Key: android.intent.extra.STREAM EXTRA 4: Class: java.lang.Integer Key: sort_order Value: 0

2 ACTIVITIES MATCH THIS INTENT: Polaris Viewer 4.1 (com.infraware.polarisviewer4 - com.infraware.polarisoffice4.OfficeLauncherActivity) Rollout PdfEditor (com.example.rolloutpdfeditor - com.example.rolloutpdfeditor.MainActivity) >

回答1:

You are missing required <category /> tags from your IntentFilter! if you look at the documentation for <category /> it says:

Note: In order to receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methods startActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULT category. If you do not declare it in your intent filter, no implicit intents will resolve to your activity.

So you always have to include android.intent.category.DEFAULT as category for the IntentFilter to work at all. If you want you app to be able to handle pdf links from a browser or other apps you also need to include android.intent.category.BROWSABLE. You can find documentation about BROWSABLE here. It reads:

CATEGORY_BROWSABLE
The target activity allows itself to be started by a web browser to display data referenced by a link — such as an image or an e-mail message.

Try this IntentFilter:

<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:mimeType="application/pdf" />
</intent-filter>

I think you are missing those two categories.