Determining Intent Actions and MIME Types Android

Let’s look at a simple example. For the remainder of this chapter, we make various modifications to a simple field notes application that uses a content provider to expose African game animal field notes; each note has a title and text body (the content itself comes from field notes on African game animals that we wrote up years ago on our nature blog, which is very popular with grade-schoolers).Throughout these examples, the application acts as a content type handler for VIEW requests for data with a custom MIME type:

Implementing the Activity to Process the Intents

Next the application needs an Activity class to handle the Intents it receives. For the sample, we simply need to load a page capable of viewing a field note. Here is a sample implementation of an Activity that can parse the Intent data and show a screen to displays the field note for a specific animal:

public class SimpleViewDetailsActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
try {
Intent launchIntent = getIntent();
Uri launchData = launchIntent.getData();
String id = launchData.getLastPathSegment();
Uri dataDetails = Uri.withAppendedPath
(SimpleFieldnotesContentProvider.CONTENT_URI, id);
Cursor cursor =
managedQuery(dataDetails, null, null, null, null);
String fieldnoteTitle = cursor.getString(cursor
String fieldnoteBody = cursor.getString(cursor
TextView fieldnoteView = (TextView)
TextView bodyView = (TextView) findViewById(;
} catch (Exception e) {
Toast.makeText(this, “Failed.", Toast.LENGTH_LONG).show();

The SimpleViewDetailsActivity class retrieves the Intent that was used to launch the Activity using the getIntent() method. It then inspects the details of that intent, extracting the specific field note identifier using the getLastPathSegment() method.The rest of the code simply involves querying the underlying content provider for the appropriate field note record and displaying it using a layout.

Registering the Intent Filter

Finally, the Activity class must be registered in the application’s Android manifest file and the intent filter must be configured so that the application only accepts Intents for specific actions and specific MIME types. For example, the SimpleViewDetailsActivity would be registered as follows:

android:name="android.intent.action.VIEW" />
android:name="android.intent.category.DEFAULT" />
<data android:mimeType =
“" />

The <activity> tag remains the same as any other. The <intent-filter> tag is what’s interesting here. First, the action that the application wants to handle is defined using an <action> tag that specifies the action the application can handle is the VIEW action. The <category> tag is set to DEFAULT, which is most appropriate, and finally the <data> tag is used to filter VIEW Intents further to only those of the custom MIME type associated with field notes.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Android Topics