Using Intent Filters to Service Implicit Intents

If an Intent is a request for an action to be performed on a set of data, how does Android know which application (and component) to use to service the request?

Intent Filters are used to register Activities, Services, and Broadcast Receivers as being capable of performing an action on a particular kind of data. Intent Filters are also used to register Broadcast Receivers as being interested in Intents broadcasting a given action or event.

Using Intent Filters, application components announce that they can respond to action requests from any application installed on the device.

Prepared for ASHLEE KABAT, email: [email protected] Order number: 56760408 This PDF is for the purchaser's personal use in accordance with the Wrox Terms of Service and under US copyright as stated on this book's copyright page. If you did not purchase this copy, please visit www.wrox.com to purchase your own copy.

To register an application component as a potential Intent handler, add an intent-filter tag to the component's manifest node using the following tags (and associated attributes) within the Intent Filter node:

> action Uses the android:name attribute to specify the name of the action being serviced. Each Intent Filter must have one (and only 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 standard values provided by Android and listed here:

> 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 where that category will always resolve to a single action using the Intent resolution described below, 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 fired 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 web site), 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.

> gadget By setting the gadget category you specify that this Activity can run embedded inside another Activity.

> 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 lets you 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 Lets you specify the type of data your component is capable of handling. For example, <type android:value="vnd.android.cursor.dir/*"/> would match any Android cursor.

> android:path Specifies valid''path'' values for the URI (/tga,nsport/boats/).

Prepared for ASHLEE KABAT, email: [email protected] Order number: 56760408 This PDF is for the purchaser's personal use in accordance with the Wrox Terms of Service and under US copyright as stated on this book's copyright page. If you did not purchase this copy, please visit www.wrox.com to purchase your own copy.

> android:port Specifies valid ports for the specified host.

> android:scheme Requires a particular scheme (e.g., content or http).

Listing 5-7 shows an Intent Filter for an Activity that can perform the show_damage action as either a primary or an alternative action (you'll create earthquake content in the next chapter).

LISTING 5-7: Registering an Activity as an Intent Receiver

Available for downloadon <activity android:name=".EarthquakeDamageViewer" android:label="View Damage"> Wrox.com <intent-filter>

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

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment