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: vnd.android.cursor.item/vnd.androidbook.live.fieldnotes

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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
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);
cursor.moveToFirst();
String fieldnoteTitle = cursor.getString(cursor
.getColumnIndex(SimpleFieldnotesContentProvider
.FIELDNOTES_TITLE));
String fieldnoteBody = cursor.getString(cursor
.getColumnIndex(SimpleFieldnotesContentProvider
.FIELDNOTES_BODY));
TextView fieldnoteView = (TextView)
findViewById(R.id.text_title);
fieldnoteView.setText(fieldnoteTitle);
TextView bodyView = (TextView) findViewById(R.id.text_body);
bodyView.setLinksClickable(true);
bodyView.setAutoLinkMask(Linkify.ALL);
bodyView.setText(fieldnoteBody);
} 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:

<activity
android:name="SimpleViewDetailsActivity">
<intent-filter>
<action
android:name="android.intent.action.VIEW" />
<category
android:name="android.intent.category.DEFAULT" />
<data android:mimeType =
“vnd.android.cursor.item/vnd.androidbook.live.fieldnotes" />
</intent-filter>
</activity>

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.



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

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

Android Topics