Creating Intent Filters in Android

Having learned to use Intents to start Activities/Services and to broadcast events, it’s important to understand how to create the Broadcast Receivers and Intent Filters that listen for Broadcast Intents and allow your application to respond to them

In the case of Activities and Services, an Intent is a request for an action to be performed on a set of data, and an Intent Filter is a declaration that a particular application component is capable of performing an action on a type of data.

Intent Filters are also used to specify the actions a Broadcast Receiver is interested in receiving.

Using Intent Filters to Service Implicit Intents

To register an Activity or Service as a potential Intent handler, add an intent-filter tag to its manifest node using the following tags (and associated attributes):

action — Uses the android:name attribute to specify the name of the action being serviced. Each Intent Filter must have at least one action tag. Actions should be unique strings that are self-describing. Best practice is to use a naming system based on the Java package naming conventions

category — Uses the android:name attribute to specify under which circumstances the action should be serviced. Each Intent Filter tag can include multiple category tags. You can specify your own categories or use the following standard values provided by Android:

  • ALTERNATIVE — This category specifies that this action should be available as an alternative to the default action performed on an item of this data type. For example, where the default action for a contact is to view it, the alternative could be to edit it
  • SELECTED_ALTERNATIVE — Similar to the ALTERNATIVE category, but whereas that category will always resolve to a single action using the intent resolution described next, SELECTED_ALTERNATIVE is used when a list of possibilities is required. As you’ll see later in this chapter, one of the uses of Intent Filters is to help populate context menus dynamically using actions.
  • BROWSABLE — Specifies an action available from within the browser. When an Intent is fi red from within the browser, it will always include the browsable category. If you want your application to respond to actions triggered within the browser (e.g., intercepting links to a particular website), you must include the browsable category.
  • DEFAULT — Set this to make a component the default action for the data type specified in the Intent Filter. This is also necessary for Activities that are launched using an explicit Intent
  • HOME — By setting an Intent Filter category as home without specifying an action, you are presenting it as an alternative to the native home screen
  • LAUNCHER — Using this category makes an Activity appear in the application launcher.

data — The data tag enables you to specify which data types your component can act on; you can include several data tags as appropriate. You can use any combination of the following attributes to specify the data your component supports:

  • android:host — Specifies a valid hostname (e.g., google.com)
  • android:mimetype — Specifies the type of data your component is capable of handling. For example, would match any Android cursor.
  • android:path — Specifies valid path values for the URI (e.g., /transport/boats/).
  • android:port — Specifies valid ports for the specified host
  • android:scheme — Requires a particular scheme (e.g., content or http).

The following snippet shows an Intent Filter for an Activity that can perform the SHOW_DAMAGE action as either a primary or an alternative action based on its mime type.

<intent-filter>
 <action
 android:name=”com.paad.earthquake.intent.action.SHOW_DAMAGE”
 />
 <category android:name=”android.intent.category.DEFAULT”/>
 <category
 android:name=”android.intent.category.SELECTED_ALTERNATIVE”/>
 <data android:mimeType=”vnd.earthquake.cursor.item/*”/>
</intent-filter>

Registering an Activity as an Intent Receiver for viewing content from a specific website using an Intent Filter

<activity android:name=”.MyBlogViewerActivity”>
 <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=”http”
 android:host=”blog.radioactiveyak.com”/>
 </intent-filter>
</activity>

Leave a Comment