Книга: Beginning Android

Stating Your Intent(ions)

Stating Your Intent(ions)

All Android components that wish to be notified via intents must declare intent filters, so Android knows which intents should go to that component. To do this, you need to add intent-filter elements to your AndroidManifest.xml file.

All of the example projects have intent filters defined, courtesy of the Android application-building script (activityCreator or the IDE equivalent). They look something like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  <activity android:name=".Now" android:label="Now">
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />

Note the intent-filter element under the activity element. Here, we declare that this activity:

• is the main activity for this application

• is in the LAUNCHER category, meaning it gets an icon in the Android main menu

Because this activity is the main one for the application, Android knows this is the component it should launch when somebody chooses the application from the main menu.

You are welcome to have more than one action or more than one category in your intent filters. That indicates that the associated component (e.g., activity) handles multiple different sorts of intents.

More than likely, you will also want to have your secondary (non-MAIN) activities specify the MIME type of data they work on. Then, if an intent is targeted for that MIME type — either directly, or indirectly by the Uri referencing something of that type — Android will know that the component handles such data.

For example, you could have an activity declared like this:

<activity android:name=".TourViewActivity">
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <data android:mimeType="vnd.android.cursor.item/vnd.commonsware.tour" />

This activity will get launched by an intent requesting to view a Uri representing a vnd.android.cursor.item/vnd.commonsware.tour piece of content. That intent could come from another activity in the same application (e.g., the MAIN activity for this application) or from another activity in another Android application that happens to know a Uri that this activity handles.

Оглавление книги

Генерация: 1.064. Запросов К БД/Cache: 3 / 1
Вверх Вниз