MY mENU


Tuesday 6 March 2012

Intents and Intent filters in Android

Intents
Intents are simple message-passing frameworks providing a facility for late run-time binding between components in the same or different applications. The Intent object itself  is a passive data structure holding an abstract description of an operation to be performed or, in the case of broadcasts, a description of something that has happened and is being announced. 

Intents are messages that describe operations to perform (such as “send an email” or “choose a photo”), or in the case of broadcasts, provide descriptions of external events that have occurred (a device’s camera being activated, for example) and are being announced. These messages are implemented as instances of the android.content.Intent class. Intents can be divided into two groups.



Explicit intents:  designate the target component by its name. Since component names would generally not be known to developers of other applications, they are typically used for application-internal messages — such as an activity starting a subordinate service or launching another activity.

Implicit intents: An implicit intent doesn't name a target (the component name is not assigned a value). Implicit intents are often used to start components in other apps. Android searches for the best component (a single activity or service to perform the requested action) or components (a set of broadcast receivers to respond to the broadcast announcement) to handle the implicit intent. During the search, Android compares the contents of the Intent object to intent filters, manifest information associated with components that can potentially receive intents. They are often used to activate components in other applications.

Intent Filters : Filters advertise a component’s capabilities and identify only those intents that the component can handle. They open up the component to the possibility of receiving implicit intents of the advertised type. If a component has no intent filters, it can receive only explicit intents. In contrast, a component with filters can receive explicit and implicit intents. Android consults an Intent object’s action, category, data, and type when comparing the intent against an intent filter. It doesn't take extras and flags into consideration.

 A component with filters can receive both explicit and implicit intents. An explicit intent is always delivered to its target, no matter what it contains; the filter is not consulted. But an implicit intent is delivered to a component only if it can pass through one of the component's filters.


An Intent object describes a message in terms of some combination of the following items:


Action: A string naming the action to be performed or, in the case of broadcast intents, the action that took place and is being reported. Actions are described by Intent constants such as ACTION_CALL (initiate a phone call), ACTION_EDIT (display data for the user to edit), and ACTION_MAIN (start up as the initial activity). You can also define your own action strings for activating the components in your app. These strings should include the app package as a prefix   ("com.example.project.SELECT_COLOR", for example).


Category: A string that provides additional information about the kind of component that should handle the intent. For example, CATEGORY_LAUNCHER means that the calling activity should appear in the device’s app launcher as a top-level app.


Component name: A string that specifies the fully qualified name (package plus name) of a component class to use for the intent. The component name is optional. If set, the Intent object is delivered to an instance of the designated class. If not set, Android uses other information in the Intent object to locate a suitable target.


Data: The uniform resource identifier of the data on which to operate (such as a person record in a contacts database).


Extras: A set of key-value pairs providing additional information that should be delivered to the component handling the intent. For example,given an action for sending an email, this information could include the message’s subject, body, and so on.


Flags: Bit values that instruct Android on how to launch an activity (for example, which task the activity should belong to) and how to treat the activity after launch (for example, whether the activity can be considered a recent activity). Flags are represented by constants in the Intent class; for example, FLAG_ACTIVITY_NEW_TASK specifies that this activity will become the start of a new task on this activity stack.


Type: The MIME type of the intent data. Normally, Android infers a type from the data. By specifying a type, you disable that inference.

No comments:

Post a Comment