Java Reference
In-Depth Information
isoptional.Whenset,the
Intent
objectisdeliveredtoaninstanceofthe
designatedclass.Whennotset,Androidusesotherinformationinthe
In-
tent
object to locate a suitable target.
•
Data
:Theuniformresourceidentifier(URI)ofthedataonwhichtooperate
(e.g., a person record in a contacts database).
•
Extras
:Asetofkey-valuepairsprovidingadditionalinformationthatshould
bedeliveredtothecomponenthandlingtheintent.Forexample,givenanac-
tionforsendingane-mailmessage,thisinformationcouldincludethemes-
sage's subject, body, and so on.
•
Flags:
Bit values that instruct Android on how to launch an activity (e.g.,
which task the activity should belong to—tasks are discussed later in this
chapter)andhowtotreattheactivityafterlaunch(e.g.,whethertheactivity
canbeconsideredarecentactivity).Flagsarerepresentedbyconstantsinthe
Intent
class;forexample,
FLAG_ACTIVITY_NEW_TASK
specifiesthat
this activity will become the start of a new task on this history stack—the
history stack is discussed later in this chapter.
•
Type
:TheMultipurposeInternetMailExtensions(MIME)typeoftheintent
data. Normally, Android infers a type from the data. By specifying a type,
you disable that inference.
Intentscanbeclassifiedasexplicitorimplicit.An
explicit intent
designatesthetarget
componentbyitsname(thepreviouslymentionedcomponentnameitemisassigned
a value). Because component names are usually unknown to the developers of oth-
erapps,explicitintentsaretypicallyusedforapp-internalmessages(e.g.,anactivity
thatlaunchesanotheractivitylocatedwithinthesameapp).Androiddeliversanex-
plicit intent to an instance ofthe designated target class. Only the
Intent
object's
component name matters for determining which component should get the intent.
An
implicit intent
doesn'tnameatarget(thecomponentnameisn'tassignedavalue).
Implicit intents are often used to start components in other apps. Android searches
forthebestcomponent(asingleactivityorservicetoperformtherequestedaction)
or components (a set of broadcast receivers to respond to the broadcast announce-
ment)tohandletheimplicitintent.Duringthesearch,Androidcomparesthecontents
ofthe
Intent
objectto
intent filters
,manifestinformationassociatedwithcompon-
ents that can potentially receive intents.
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
implicitintentsoftheadvertisedtype.Whenacomponenthasnointentfilters,itcan
receiveonlyexplicitintents.Incontrast,acomponentwithfilterscanreceiveexplicit
andimplicitintents.Androidconsultsan
Intent
object'saction,category,data,and