/ itsummaries

AndroidDev: IntentFilter

As I found those to be annoying as hell, here a small post about my best-practise of implementing IntentFilters.

The easiest way to get a hold on everything you want to cover is the following:

  1. Start up your Android Device/Simulator and your IDE, so you can get LogCat up and running.

  2. Setup LogCat to not filter anything, so you get the full view of what's going on

  3. Open (e.g.) whatever file you want your application to handle later on

  4. Note and write down the Intent. It will look like this :
    Failed to start Intent {act=android.intent.action.VIEW dat=content://downloads/all_downloads/17 typ=application/sla flg=0x10000003 }: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://downloads/all_downloads/17 typ=application/sla flg=0x10000003 }

    The important parts are those:

    act=android.intent.action.VIEW

    dat=content://downloads/all_downloads/17

    typ=application/sla

  5. Remember to do this with every way you want your application to handle things. This includes opening files from web, from the download list or from a dedicated filemanager. All might provide different IntentFilters

  6. Now it's time to create the filters. Head to your AndroidManifest.xml and start creating IntentFilters like this:

       <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:host="*" />
           <data android:mimeType="application/sla" />
       </intent-filter>
    

    The sheme can be seen in the dat part of the Intent, the host is * most of the time and the mimeType is the typ.

    Create a filter for every different Intent you wrote down above.

Edit: Please always keep in mind, that activity-intents are not really standardised. The Intents you will come along vary widely and even the way you put the in the Manifest matters. Be aware of this and be prepared to do some research if you want to get this done right. Also don't rely on emulators, I found that most of them work fine, while real devices tend to be more error-prone.

I hope this made everything a little more transparent.
If it did not, here are some additional sources:

https://developer.android.com/guide/components/intents-filters.html

https://stackoverflow.com/questions/4799576/register-new-file-type-in-android

https://stackoverflow.com/questions/7806807/associate-file-type-with-android-app-fails

https://publish.illinois.edu/weiyang-david/2013/04/11/how-to-write-intent-filter-dealing-with-mime-type-problem/