Login     Sign Up
Cyril Sermon (@admin)
10 months ago
51 Views

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.

Using Intent Filters, application components tell Android that they can service action requests from oth-ers, including components in the same, native, or third-party applications.

To register an application component as an Intent handler, use the intent-filter tag within the com-ponent’s manifest node.

Using the following tags (and associated attributes) within the Intent Filter node, you can specify a component’s supported actions, categories, and data:

❑action Use the android:name attribute to specify the name of the action being serviced. Actions should be unique strings, so best practice is to use a naming system based on the Java package naming conventions.

❑category Use the android:category 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 below:

❑ALTERNATIVE As you’ll see later in this chapter, one of the uses of Intent Filters is to help populate context menus with actions. The alternative 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 alternatives could be to edit or delete it.

❑SELECTED_ALTERNATIVE Similar to the alternative category, but where Alterna-tive will always resolve to a single action using the Intent resolution described below, SELECTED_ALTERNATIVE is used when a list of possibilities is required.

❑BROWSABLE Specifies an action available from within the browser. When an Intent is fired from within the browser, it will always specify the browsable category.

❑DEFAULT Set this to make a component the default action for the data values defined by 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 embed-ded inside another Activity.

❑HOME The home Activity is the first Activity displayed when the device starts (the launch screen). 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 matches for data your component can act on; you can include several schemata if your component is capable of handling more than one. You can use any combination of the following attributes to specify the data that your component supports:

❑android:host Specifies a valid host name (e.g., com.google).

❑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 Valid “path” values for the URI (e.g., /transport/boats/) ❑ android:port Valid ports for the specified host

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

The following code snippet shows how to configure an Intent Filter for an Activity that can perform the SHOW_DAMAGE action as either a primary or alternative action. (You’ll create earthquake content in the next chapter.)

<activity android:name=”.EarthquakeDamageViewer”

android:label=”View Damage”>

<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>