Datasheet

14
Part I: The Nuts and Bolts of Android
Activities
Android applications are made up of one or more activities. Your app must
contain at least one activity, but an Android application can contain several.
Think of an activity as being a container for your UI, holding your UI as well
as the code that runs it. It’s kind of like a form, for you Windows program-
mers out there. I discuss activities in more detail in Chapters 3 and 5.
Intents
Intents make up the core message system that runs Android. An intent is
composed of an action that it needs to perform (View, Edit, Dial, and so on)
and data. The action is the general action to be performed when the intent is
received, and the data is the data to operate on. The data might be a contact
item, for example.
Intents are used to start activities and to communicate among various parts
of the Android system. Your application can either broadcast an intent or
receive an intent.
Sending messages with intents
When you broadcast an intent, you’re sending a message telling Android to
make something happen. This intent could tell Android to start a new activity
from within your application, or it could start a different application.
Registering intent receivers
Just because you send a message doesn’t mean that something will happen
automatically. You have to register an intent receiver that listens for the
intent and then tells Android what to do, whether the task is starting a new
activity or starting a different app. If many receivers can accept a given
intent, a chooser can be created to allow the user to pick the app she wants
to use. A classic example is long-pressing an image in an image gallery. Long-
pressing means clicking something for a long time to bring up a context menu.
By default, various registered receivers handle the image-sharing intents. One
of many is e-mail, and another is the messaging application (among various
other installed applications). Because you find more than one possible intent
receiver, the user is presented with a chooser asking him what he should do:
use e-mail, messaging, or another application, as shown in Figure 1-1.
05_9780470770184-ch01.indd 1405_9780470770184-ch01.indd 14 11/2/10 8:51 AM11/2/10 8:51 AM