Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android intent filter: associate app with file extension

I have a custom file type/extension that I want to associate my app with.

As far as I know, the data element is made for this purpose, but I can't get it working. http://developer.android.com/guide/topics/manifest/data-element.html According to the docs, and a lot of forum posts, it should work like this:

<intent-filter>     <action android:name="android.intent.action.MAIN" />     <category android:name="android.intent.category.DEFAULT" />     <category android:name="android.intent.category.BROWSABLE" />     <data android:mimeType="application/pdf" /> </intent-filter> 

Well, it does not work. What did I do wrong? I simply want to declare my own file type.

like image 520
Tamas Avatar asked Sep 21 '10 12:09

Tamas


People also ask

What is difference between Intent and Intent filter in Android?

An intent is an object that can hold the os or other app activity and its data in uri form.It is started using startActivity(intent-obj).. \n whereas IntentFilter can fetch activity information on os or other app activities.

What is the use of Intent filter in the Androidmanifest XML file?

An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to receive. For instance, by declaring an intent filter for an activity, you make it possible for other apps to directly start your activity with a certain kind of intent.

What is Android Intent category browsable?

Include the BROWSABLE category. It is required in order for the intent filter to be accessible from a web browser. Without it, clicking a link in a browser cannot resolve to your app. Also include the DEFAULT category. This allows your app to respond to implicit intents.

What is the purpose of category in an Intent filter?

Categories are used for implicit Intents. So, If your Activity can be started by an implicit Intent when no other specific category is assigned to activity, activity's Intent filter should include this category. (even if you have other categories in the Intent filter).


1 Answers

You need multiple intent filters to address different situation you want to handle.

Example 1, handle http requests without mimetypes:

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

Handle with mimetypes, where the suffix is irrelevant:

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

Handle intent from a file browser app:

  <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> 
like image 134
Phyrum Tea Avatar answered Oct 14 '22 21:10

Phyrum Tea