For my app I would like to be able to open files from email attachments or file browsers, and have them be processed in the background - perhaps with a Toast providing success/failure feedback. I have the following intent-filters setup:
<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/octet-stream"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<action android:name="android.intent.action.EDIT"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data
android:mimeType="*/*"
android:host="*"
android:pathPattern=".*\\.mpb"
android:scheme="file"/>
</intent-filter>
However, if I place those filters under a BroadcastReceiver, Android seemingly refuses to acknowledge the existence of those filters. It works properly if I place the exact same filters under an Activity. Is this a limitation of Android intent-filters?
For now I am simply using the Activity to pass the intent along to the IntentService in which I process the file - much the same as what my BroadcastReceiver would've done - and then call finish()
and return;
. This gets the job done, but is a bad user experience because an activity appears instantaneously and disappears. I'd much rather get the BroadcastReceiver to work, so is there something I'm missing?
is there something I'm missing? - Yes, I afraid there is.
the reason you don't receive broadcast with the same intent filter that activity do catch is simple:
the application that triggers this event (gmail application in your example) is starting implicit activity , and not sending broadcast.
what means that if no broadcast is been sent - then you can forget about receive it via BroadcastReceiver
no work around that, instead of launching your own activity.
starting implicit activity instead of sending broadcast with ACTION_VIEW
is making sense, because after all - ACTION_VIEW
is all about showing something, so it won't make sense that in reaction to user click on file - something will happened only in background.
you right that launching and imidatly finishing activity in that case is a bad user experience, but not showing anything visible is event worse - the user will continue clicking because he think that nothing happens...
what I would do instead of you - show special meaningful activity that visualize whatever processing you are doing instead of doing it only in background, and when the process finish - finish also the activity.