The lifecycle for a service is similar to that for an activity, but different in a few important
onCreate and onStart differences
Services can be started when a client calls the Context.startService(Intent)
method. If the service isn’t already running, Android starts it and calls its
onCreate method followed by the onStart method. If the service is already running,
its onStart method is invoked again with the new intent. So it’s quite possible and
normal for a service’s onStart method to be called repeatedly in a single run of the
onResume, onPause, and onStop are not needed
Recall that a service generally has no user interface, so there isn’t any need for the
onPause, onResume, or onStop methods. Whenever a service is running, it is always
in the background.
If a client needs a persistent connection to a service, it can call the Context.bind
Service method. This creates the service if it is not running, and calls onCreate but
not onStart. Instead, the onBind method is called with the client’s intent, and it
returns an IBind object that the client can use to make further calls to the service.
It’s quite normal for a service to have clients starting it and clients bound to it at
the same time.
As with an activity, the onDestroy method is called when the service is about to be
terminated. Android will terminate a service when there are no more clients starting
or bound to it. As with activities, Android may also terminate a service when
memory is getting low. If that happens, Android will attempt to restart the service
when the memory pressure passes, so if your service needs to store persistent information
for that restart, it’s best to do so in the onStart method.
How This Book Fits Together
Android is a sophisticated platform whose parts all work together: drawing and layout,
inter-process communication and data storage, search and location. Introducing it in
pieces is a challenge, but we’ve entertained the conceit of introducing the complexities
of the platform in a linear order.
The platform is also so rich that we can’t hope to show you how to use everything you
want, or even a large subset of its capabilities. We expect you to consult the official
documentation while reading this book and trying the examples. You should also use
other online resources—but be careful about web pages or forum postings that have
been around a while, because interfaces change. There is also a substantial amount of
misinformation out on the Web; we discovered scads of it while writing the book.
This book is written for experienced developers who want to quickly learn what they
need to know to build Android applications. The book is written with references to an
example application (MJAndroid, discussed in much more detail in the next chapter)
that you can freely download and reuse. The major topics covered in the book include:
New Android concepts
Android builds upon a lot of legacy technology (Java, Linux, and the Internet, just
to name a few), but it also introduces some new concepts needed to enable the
Android development environment
We’ll show how to install the free, open source Android development environment
on your own system, and how to use that environment to develop, test, and debug
your own applications. You’ll not only learn the mechanics of using the system,
but also what’s going on behind the scenes, so you’ll have a better understanding
of how the whole system fits together.
Android user interface
The Android user interface elements are similar to things you’ve seen before, but
also different. We’ll show you what the principal elements are, how they’re used,
and what they look like on the screen. We’ll also show you the basic layout types
available for the Android screen.
Android makes it easy to leverage existing applications through the use of Intents.
For example, if you want to dial a phone number, you don’t have to do all the work
in your application, or even know what applications are available that know how
to dial. You can just ask Android to find you an installed application that knows
how to dial out, and pass it the string of numbers.
Location-based services and mapping
As you’d expect from a Google-sponsored environment, mapping and location are
major features of Android. You’ll see how easy it is to create sophisticated mapping
and location-based applications.
Android includes the SQLite database libraries and tools, which your application
can use to store persistent data. Content providers, which we’ve already introduced,
provide data to other applications. Using the libraries can be a little tricky,
but in Chapter 8 we’ll guide you through the creation of a database, and reading,
writing, and deleting data records.
Your application has access to 2D and 3D graphics capabilities in Android. Animation
and various advanced effects are also provided. This book will show you
how to use those libraries so you can build a compelling user interface for your
Android, even more than most smartphone operating systems, places great emphasis
on communication—by voice, by text messaging, by instant messaging, and
by Internet. You’ll see how your application can take advantage of these capabilities
so your users can become part of a larger community of data and users.
The next three chapters, Chapters 2 through 4, set you up with a working application,
and will give you a sense of how the files and basic classes fit together. Chapter 5
empowers you to better understand what you’re doing and helps you debug your first
The Android toolkit naturally comes with an enormous number of working code examples
in its ApiDemos application. Unfortunately, its very size and sophistication
make it a formidable castle for novices to enter. Chapter 6 guides you through it.
A bit of experience with ApiDemos will convince you that you need some more background
and tutorial help. In Chapter 7, we’ll show you how to sign and publish your
application, which you need to do in order to test it with Google Maps, even before
you’re ready to go public.
Chapter 8 presents tutorials on two data storage systems.
Chapter 9 presents location and mapping, which are key features that draw people to
mobile devices and which you’ll surely want to incorporate into your application.
We then turn to a critical part of any end-user application, graphics, in three
information-packed chapters, Chapters 10 through 12.
Chapter 13 takes another step into the complexity and unique power of Android, by
discussing how applications can offer functionality to other applications. This allows
for powerful mashups, which involve one program standing on the shoulders of other
Let’s not forget that Android runs on telephones. Chapters 14 and 15 wrap up the book
by showing you how to place and track phone calls.
There’s even more to Android than these features, of course, but programmers of all
stripes will find in this book what they need to create useful and efficient programs for
the Android platform.
Components of an Android Application
Android Activity Lifecycle
Android Service Lifecycle
Setting Up Your Development Environment
Main Sample Application
Downloading the MJAndroid Code
Building and Running the MicroJobs Application
Digging a Little Deeper
Running an Application
Startup Code and Resources in the MJAndroid Application
Initialization in MicroJobs
More Initialization of MicroJobs.
Debugging Android Applications
Writing your own logcat entries
Dalvik Debug Monitor Service
The ApiDemos Application
Finding the Source to an Interesting Example
Adding Your Own Examples to ApiDemos
Signing and Publishing Your Application
Attach an End User License Agreement If Desired
Getting a Signing Certificate for an Application You Are Going to Ship
Getting a Signing Certificate While Debugging
Signing Your Application
Persistent Data Storage
Basic Structure of the MicroJobsDatabase Class
Reading Data from the Database
Updating data already in the database
Consuming a Content Provider
The Google Maps Activity
MapView and MyLocationOverlay Initialization
Pausing and Resuming a MapActivity
Controlling the Map with the KeyPad
Connecting to a Location Provider
Updating the Emulated Location
Building a View
Assembling a Graphical Interface
Wiring Up the Controller
Listening to the Model
Listening for Touch Events
Alternative Ways to Handle Events
A Widget Bestiary
Adapters and AdapterViews
Drawing 2D and 3D Graphics
Inter Process Communication
Getting a Result via Inter Process Communication
Android Interface Definition Language
Classes Underlying AIDL
Android IPC Compared with Java Native Interface
Simple Phone Calls
Exploring the Phone Code Through the Debugger
Telephony State Information and Android Telephony Classes
Android Telephony Internals
The android Package
Exploring Android Telephony Internals