28-09-2012, 12:26 PM
Intent & Intent Filters
android_intent.pdf (Size: 1.11 MB / Downloads: 65)
What is Intent Messaging?
• Three of the core components of an Android
application - activities, services, and broadcast
receivers - are activated through messages,
called intents
> One activity starts another activity by
creating/sending an Intent
• Means of leveraging activities, services, and
broadcast receivers of other applications
> Application #1 can use “Photo display” activity of
Application #2 (or system) by creating/sending an
intent
• Intent messaging is a facility for late run-time
binding between components in the same or
different applications
> Enables a flexible and agile architecture: A
component that performs a task is selected during
runtime
> One activity can start another activity by creating an
intent that says “Display web browser” - Android
runtime then selects a best qualified “Display web
browser” activity among the possible candidates
during runtime through so-called “Intent resolution”
(We will talk about this in detail later in this
presentation)
What Does Intent Object Contain?
• Information of interest to the target component
that receives the intent
> Action to be taken
> Data to act on
• Information of interest to the Android system
> Category of component that should handle the intent
> Instructions on how to launch a target activity
Component name Field
• Specifies the name of the component (name of
the activity if the component is activity) that
should handle the intent
> Class name of the target component (for example
"com.javapassion.ForwardTargetActivity")
• Setting component name is optional
> If it is set, the Intent object is delivered to an
instance of the designated class.
> If it is not set, Android uses other information in the
Intent object to locate a suitable target - this is called
“intent resolution” (We will talk about this later in
this presentation)
Action Field
• A string naming the action to be performed
• The Intent class defines a number of predefined
action constants, including
> ACTION_CALL, ACTION_EDIT, ACTION_MAIN,
ACTION_SYNC, ACTION_BATTERY_LOW, etc.
• You can also define your own action strings for
activating the components in your application
• The action largely determines how the rest of
the intent is structured - particularly the data
and extras fields - much as a method name
determines a set of arguments and a return
value.
Data Field
• The URI of the data to be acted on and the
MIME type of that data.
• Different actions are paired with different kinds
of data specifications.
> If the action field is ACTION_EDIT, the data field
would contain the URI of the document to be
displayed for editing.
> If the action is ACTION_CALL, the data field would be
a tel: URI with the number to call.
> If the action is ACTION_VIEW and the data field is an
http: URI, the receiving activity would be called upon
to download and display whatever data the URI
refers to.