224 CHAPTER 7: Making Apps Interactive: Intents, Event Handling, and Menus
Using Intent Objects: The Android Intent Class
An Intent object is used within the Android OS as a messaging construct that you can dispatch to
request some sort of “action” from one of the other components within your Android application, or
even within someone else’s Android application, which is external to your own Android application.
There are three primary uses for Intent objects in Android; launching Activity subclasses, starting
Service subclasses, and sending messages around an Android application, or even to external
Android applications by using Broadcast Receiver classes and methods.
Here’s a basic diagram which shows how your Activity uses Intent objects:
Activity > Intent object containing start Activity Intent > Activity (launched) or:
Activity > Intent object containing start Service Intent > Service (launched)
As you know, the Activity subclasses each represent a single user interface screen within an app’s
functionality. You will start each new instance of an Activity subclass by passing an Intent object over
to the .startActivity( ) method, which we will be doing in the next step in the menu implementation
work process.
The Intent object passed into the .startActivity( ) method will contain data which prescribes which of
the app’s Activity subclass you want to start, and carries the Context object describing the current
context for your application. We will go over the concept of Context within this chapter as well, as it
is used often in Android programming.
You can also use an Intent object to start a Service subclass; we’ll be covering Services in
Chapter 13. A Service is an Android component which can perform operations in the background.
These operations usually do not need a user interface, and can be performed “asynchronously,” or
out of sync, with the user’s normal “flow of use” of your application and its purpose and objectives.
An example of this would be playing a background music track.
You can also start a Service subclass using an Intent object in order to perform a one-time operation
such as the downloading of a file, again by passing an Intent object over to a .startService( ) method
call. This Intent should describe the Service subclass to start, as well as any necessary data that is
needed for the Service to process.
You can also use an Intent object to deliver a broadcast across the Android OS, to anything running
under it. A broadcast in Android is a term for any message that an app running on the same
Android device as your app can receive. The Android operating system will deliver broadcasts for
system events, such as when the system boots up, or when the device is plugged in and starts
charging. You can deliver a broadcast message to other apps by passing Intent objects via a
.sendBroadcast( ), a .sendOrderedBroadcast( ), or a .sendStickyBroadcast( ) method call.
There are two types of Intent objects you can create and utilize within the Android OS: explicit Intent
objects and implicit Intent objects. Explicit Intent objects will specifically reference the Android
application component to start, by using a precise application component name and component
type. When you create an explicit Intent object to start an Activity subclass or Service subclass, the
Android operating system starts the component specified in that Intent object.
In the next section, you will start your EditGalaxy.class using this fully qualified compiled class
name. A fully qualified compiled class name would be referenced in your code as EditGalaxy.class,
which is different than the fully qualified code class name, which would be referenced as