0% found this document useful (0 votes)
31 views

Android Book

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Android Book

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 135

SAMPLE EDITION - full edition at http://commonsware.

com/Android/
The Busy Coder's Guide to Android
Development

by Mark L. Murphy

SAMPLE EDITION - full edition at http://commonsware.com/Android/


The Busy Coder's Guide to Android Development
by Mark L. Murphy

Copyright © 2008 CommonsWare, LLC. All Rights Reserved.


Printed in the United States of America.

CommonsWare books may be purchased in printed (bulk) or digital form for educational or
business use. For more information, contact [email protected].

Printing History:
Jul 2008: Version 1.0 ISBN: 978-0-9816780-0-9

The CommonsWare name and logo, “Busy Coder's Guide”, and related trade dress are
trademarks of CommonsWare, LLC.

All other trademarks referenced in this book are trademarks of their respective firms.

The publisher and author(s) assume no responsibility for errors or omissions or for damages
resulting from the use of the information contained herein.

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Table of Contents

Welcome to the Warescription!..................................................................................xiii


Preface..........................................................................................................................xv
Welcome to the Book!...........................................................................................................xv
Prerequisites..........................................................................................................................xv
Warescription.......................................................................................................................xvi
Book Bug Bounty.................................................................................................................xvii
Source Code License..........................................................................................................xviii
Creative Commons and the Four-to-Free (42F) Guarantee............................................xviii
The Big Picture................................................................................................................1
What Androids Are Made Of.................................................................................................3
Activities...........................................................................................................................3
Content Providers...........................................................................................................4
Intents..............................................................................................................................4
Services.............................................................................................................................4
Stuff At Your Disposal.............................................................................................................5
Storage..............................................................................................................................5
Network............................................................................................................................5
Multimedia.......................................................................................................................5
GPS...................................................................................................................................5
Phone Services.................................................................................................................6
Project Structure............................................................................................................7
Root Contents..........................................................................................................................7
The Sweat Off Your Brow.......................................................................................................8

iii

SAMPLE EDITION - full edition at http://commonsware.com/Android/


And Now, The Rest of the Story.............................................................................................8
What You Get Out Of It.........................................................................................................9
Inside the Manifest........................................................................................................11
In The Beginning, There Was the Root, And It Was Good.................................................11
Permissions, Instrumentations, and Applications (Oh, My!).............................................12
Your Application Does Something, Right?..........................................................................13
Creating a Skeleton Application...................................................................................17
Begin at the Beginning...........................................................................................................17
The Activity............................................................................................................................18
Dissecting the Activity...........................................................................................................19
Building and Running the Activity.......................................................................................21
Using XML-Based Layouts............................................................................................23
What Is an XML-Based Layout?...........................................................................................23
Why Use XML-Based Layouts?............................................................................................24
OK, So What Does It Look Like?..........................................................................................25
What's With the @ Signs?....................................................................................................26
And We Attach These to the Java...How?...........................................................................26
The Rest of the Story.............................................................................................................27
Employing Basic Widgets.............................................................................................29
Assigning Labels....................................................................................................................29
Button, Button, Who's Got the Button?..............................................................................30
Fleeting Images......................................................................................................................31
Fields of Green. Or Other Colors..........................................................................................31
Just Another Box to Check....................................................................................................34
Turn the Radio Up.................................................................................................................37
It's Quite a View....................................................................................................................39
Useful Properties...........................................................................................................39
Useful Methods..............................................................................................................39
Working with Containers.............................................................................................41
Thinking Linearly..................................................................................................................42
Concepts and Properties...............................................................................................42
Example..........................................................................................................................45
All Things Are Relative.........................................................................................................50

iv

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Concepts and Properties...............................................................................................50
Example..........................................................................................................................53
Tabula Rasa............................................................................................................................56
Concepts and Properties...............................................................................................56
Example..........................................................................................................................59
Scrollwork..............................................................................................................................60
Using Selection Widgets...............................................................................................65
Adapting to the Circumstances............................................................................................65
Using ArrayAdapter......................................................................................................66
Other Key Adapters.......................................................................................................67
Lists of Naughty and Nice....................................................................................................68
Spin Control...........................................................................................................................70
Grid Your Lions (Or Something Like That...).....................................................................74
Fields: Now With 35% Less Typing!.....................................................................................78
Galleries, Give Or Take The Art...........................................................................................82
Employing Fancy Widgets and Containers..................................................................83
Pick and Choose....................................................................................................................83
Time Keeps Flowing Like a River.........................................................................................88
Making Progress....................................................................................................................89
Putting It On My Tab...........................................................................................................90
The Pieces.......................................................................................................................91
The Idiosyncrasies..........................................................................................................91
Wiring It Together........................................................................................................93
Other Containers of Note.....................................................................................................96
Applying Menus............................................................................................................97
Flavors of Menu.....................................................................................................................97
Menus of Options.................................................................................................................98
Menus in Context................................................................................................................100
Taking a Peek.......................................................................................................................102
Embedding the WebKit Browser................................................................................107
A Browser, Writ Small.........................................................................................................107
Loading It Up.......................................................................................................................109
Navigating the Waters..........................................................................................................111

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Entertaining the Client.........................................................................................................111
Settings, Preferences, and Options (Oh, My!)....................................................................114
Showing Pop-Up Messages..........................................................................................117
Raising Toasts........................................................................................................................117
Alert! Alert!............................................................................................................................118
Checking Them Out.............................................................................................................119
Dealing with Threads..................................................................................................123
Getting Through the Handlers............................................................................................123
Messages.......................................................................................................................124
Runnables.....................................................................................................................127
Running In Place..................................................................................................................127
Utilities (And I Don't Mean Water Works).......................................................................128
And Now, The Caveats........................................................................................................128
Handling Activity Lifecycle Events..............................................................................131
Schroedinger's Activity.........................................................................................................131
Life, Death, and Your Activity.............................................................................................132
onCreate() and onCompleteThaw()............................................................................132
onStart(), onRestart(), and onResume().....................................................................133
onPause(), onFreeze(), onStop(), and onDestroy()...................................................134
Using Preferences........................................................................................................137
Getting What You Want......................................................................................................137
Stating Your Preference.......................................................................................................138
A Preference For Action......................................................................................................138
Accessing Files.............................................................................................................143
You And The Horse You Rode In On.................................................................................143
Readin' 'n Writin'.................................................................................................................147
Working with Resources..............................................................................................151
The Resource Lineup............................................................................................................151
String Theory........................................................................................................................152
Plain Strings..................................................................................................................152
String Formats..............................................................................................................153
Styled Text.....................................................................................................................153
Styled Formats..............................................................................................................154

vi

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Got the Picture?...................................................................................................................158
XML: The Resource Way.....................................................................................................160
Miscellaneous Values...........................................................................................................163
Dimensions...................................................................................................................163
Colors............................................................................................................................164
Arrays............................................................................................................................165
Different Strokes for Different Folks..................................................................................166
Managing and Accessing Local Databases...................................................................171
A Quick SQLite Primer........................................................................................................172
Start at the Beginning..........................................................................................................173
Setting the Table..................................................................................................................174
Makin' Data..........................................................................................................................174
What Goes Around, Comes Around...................................................................................176
Raw Queries..................................................................................................................176
Regular Queries............................................................................................................177
Building with Builders.................................................................................................177
Using Cursors...............................................................................................................179
Change for the Sake of Change...................................................................................179
Making Your Own Cursors..........................................................................................180
Data, Data, Everywhere.......................................................................................................180
Leveraging Java Libraries............................................................................................183
The Outer Limits..................................................................................................................183
Ants and Jars........................................................................................................................184
Communicating via the Internet................................................................................187
REST and Relaxation............................................................................................................187
HTTP Operations via Apache Commons...................................................................188
Parsing Responses........................................................................................................190
Stuff To Consider.........................................................................................................192
Email over Java.....................................................................................................................193
Creating Intent Filters................................................................................................199
What's Your Intent?............................................................................................................200
Pieces of Intents..........................................................................................................200
Stock Options...............................................................................................................201

vii

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Intent Routing.............................................................................................................202
Stating Your Intent(ions)....................................................................................................203
Narrow Receivers.................................................................................................................205
Launching Activities and Sub-Activities.....................................................................207
Peers and Subs.....................................................................................................................208
Start 'Em Up........................................................................................................................208
Make an Intent............................................................................................................209
Make the Call...............................................................................................................209
Finding Available Actions via Introspection...............................................................215
Pick 'Em................................................................................................................................216
Adaptable Adapters.............................................................................................................220
Would You Like to See the Menu?.....................................................................................223
Asking Around.....................................................................................................................225
Using a Content Provider...........................................................................................229
Pieces of Me.........................................................................................................................229
Getting a Handle.................................................................................................................230
Makin' Queries.....................................................................................................................231
Adapting to the Circumstances..........................................................................................233
Doing It By Hand.................................................................................................................235
Position.........................................................................................................................235
Getting Properties.......................................................................................................236
Setting Properties........................................................................................................237
Give and Take......................................................................................................................238
Beware of the BLOB!...........................................................................................................239
Building a Content Provider.......................................................................................241
First, Some Dissection.........................................................................................................241
Next, Some Typing..............................................................................................................242
Step #1: Create a Provider Class..........................................................................................243
ContentProvider..........................................................................................................243
DatabaseContentProvider...........................................................................................252
Step #2: Supply a Uri...........................................................................................................252
Step #3: Declare the Properties..........................................................................................252
Step #4: Update the Manifest.............................................................................................253

viii

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Notify-On-Change Support................................................................................................254
Requesting and Requiring Permissions.....................................................................257
Mother, May I?....................................................................................................................258
Halt! Who Goes There?.......................................................................................................259
Enforcing Permissions via the Manifest....................................................................260
Enforcing Permissions Elsewhere...............................................................................261
May I See Your Documents?...............................................................................................262
Creating a Service........................................................................................................263
Getting Buzzed....................................................................................................................264
Service with Class................................................................................................................264
When IPC Attacks!..............................................................................................................266
Write the AIDL............................................................................................................267
Implement the Interface.............................................................................................268
Manifest Destiny.................................................................................................................270
Where's the Remote?...........................................................................................................271
Invoking a Service.......................................................................................................273
Bound for Success...............................................................................................................274
Request for Service..............................................................................................................276
Prometheus Unbound.........................................................................................................276
Manual Transmission..........................................................................................................276
Alerting Users Via Notifications.................................................................................279
Types of Pestering...............................................................................................................279
Hardware Notifications..............................................................................................280
Icons..............................................................................................................................281
Letting Your Presence Be Felt.............................................................................................281
Accessing Location-Based Services.............................................................................287
Location Providers: They Know Where You're Hiding....................................................288
Finding Yourself..................................................................................................................288
On the Move........................................................................................................................292
Are We There Yet? Are We There Yet? Are We There Yet?............................................292
Testing...Testing..................................................................................................................296
Mapping with MapView and MapActivity..................................................................299
The Bare Bones....................................................................................................................299

ix

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Exercising Your Control.......................................................................................................301
Zoom.............................................................................................................................301
Center...........................................................................................................................302
Reticle...........................................................................................................................303
Traffic and Terrain...............................................................................................................303
Follow You, Follow Me........................................................................................................305
Layers Upon Layers.............................................................................................................307
Overlay Classes............................................................................................................308
Drawing the Overlay...................................................................................................308
Handling Screen Taps..................................................................................................310
Playing Media..............................................................................................................313
Get Your Media On..............................................................................................................314
Making Noise........................................................................................................................315
Moving Pictures....................................................................................................................321
Handling Telephone Calls..........................................................................................325
No, No, No – Not That IPhone...........................................................................................326
What's Our Status?..............................................................................................................326
You Make the Call!..............................................................................................................326
Searching with SearchManager...................................................................................333
Hunting Season....................................................................................................................333
Search Yourself.....................................................................................................................335
Craft the Search Activity.............................................................................................336
Update the Manifest....................................................................................................340
Try It Out.....................................................................................................................342
The TourIt Sample Application..................................................................................347
Installing TourIt..................................................................................................................347
Demo Location Provider.............................................................................................347
SD Card Image with Sample Tour..............................................................................348
Running TourIt....................................................................................................................349
Main Activity................................................................................................................350
Configuration Activity.................................................................................................352
Cue Sheet Activity.......................................................................................................354
Map Activity.................................................................................................................355

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Tour Update Activity...................................................................................................357
Help Activity................................................................................................................358
TourIt's Manifest.................................................................................................................359
TourIt's Content..................................................................................................................360
Data Storage.................................................................................................................361
Content Provider..........................................................................................................361
Model Classes...............................................................................................................361
TourIt's Activities................................................................................................................362
TourListActivity...........................................................................................................362
TourViewActivity.........................................................................................................363
TourMapActivity..........................................................................................................367
TourEditActivity..........................................................................................................367
HelpActivity.................................................................................................................367
ConfigActivity..............................................................................................................368

xi

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Preface

Welcome to the Book!

Thanks!

Thanks for your interest in developing applications for Android!


Increasingly, people will access Internet-based services using so-called
"non-traditional" means, such as mobile devices. The more we do in that
space now, the more that people will help invest in that space to make it
easier to build more powerful mobile applications in the future. Android is
new – at the time of this writing, there are no shipping Android-powered
devices – but it likely will rapidly grow in importance due to the size and
scope of the Open Handset Alliance.

And, most of all, thanks for your interest in this book! I sincerely hope you
find it useful and at least occasionally entertaining.

Prerequisites

If you are interested in programming for Android, you will need at least
basic understanding of how to program in Java. Android programming is
done using Java syntax, plus a class library that resembles a subset of the
Java SE library (plus Android-specific extensions). If you have not
programmed in Java before, you probably should quick learn how that
works before attempting to dive into programming for Android.

iii

SAMPLE EDITION - full edition at http://commonsware.com/Android/


The book does not cover in any detail how to download or install the
Android development tools, either the Eclipse IDE flavor or the standalone
flavor. The Android Web site covers this quite nicely. The material in the
book should be relevant whether you use the IDE or not. You should
download, install, and test out the Android development tools from the
Android Web site before trying any of the examples listed in this book.

Some chapters may reference material in previous chapters, though usually


with a link back to the preceding section of relevance.

Warescription

This book will be published both in print and in digital (ebook) form. The
ebook versions of all CommonsWare titles are available via an annual
subscription – the Warescription.

The Warescription entitles you, for the duration of your subscription, to


ebook forms of all CommonsWare titles, not just the one you are reading.
Presently, CommonsWare offers PDF and Kindle; other ebook formats will
be added based on interest and the openness of the format.

Each subscriber gets personalized editions of all editions of each title: both
those mirroring printed editions and in-between updates that are only
available in ebook form. That way, your ebooks are never out of date for
long, and you can take advantage of new material as it is made available
instead of having to wait for a whole new print edition. For example, when
new releases of the Android SDK are made available, this book will be
quickly updated to be accurate with changes in the APIs.

From time to time, subscribers will also receive access to subscriber-only


online material, both short articles and not-yet-published new titles.

Also, if you own a print copy of a CommonsWare book, and it is in good


clean condition with no marks or stickers, you can exchange that copy for a
discount off the Warescription price.

If you are interested in a Warescription, visit the Warescription section of


the CommonsWare Web site.

iv

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Book Bug Bounty

Find a problem in one of our books? Let us know!

Be the first to report a unique concrete problem, and we'll give you a coupon
for a six-month Warescription as a bounty for helping us deliver a better
product. You can use that coupon to get a new Warescription, renew an
existing Warescription, or give the coupon to a friend, colleague, or some
random person you meet on the subway.

By "concrete" problem, we mean things like:

• Typographical errors
• Sample applications that do not work as advertised, in the
environment described in the book
• Factual errors that cannot be open to interpretation

By "unique", we mean ones not yet reported. Each book has an errata page
on the CommonsWare Web site; most known problems will be listed there.

We appreciate hearing about "softer" issues as well, such as:

• Places where you think we are in error, but where we feel our
interpretation is reasonable
• Places where you think we could add sample applications, or expand
upon the existing material
• Samples that do not work due to "shifting sands" of the underlying
environment (e.g., changed APIs with new releases of an SDK)

However, those "softer" issues do not qualify for the formal bounty program.

Questions about the bug bounty, or problems you wish to report for bounty
consideration, should be sent to [email protected].

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Source Code License

The source code samples shown in this book are available for download
from the CommonsWare Web site. All of the Android projects are licensed
under the Apache 2.0 License, in case you have the desire to reuse any of it.

Creative Commons and the Four-to-Free


(42F) Guarantee

Each CommonsWare book edition will be available for use under the
Creative Commons Attribution-Noncommercial-Share Alike 3.0 license as of
the fourth anniversary of its publication date, or when 4,000 copies of the
edition have been sold, whichever comes first. That means that, once four
years have elapsed (perhaps sooner!), you can use this prose for non-
commercial purposes. That is our Four-to-Free Guarantee to our readers and
the broader community. For the purposes of this guarantee, new
Warescriptions and renewals will be counted as sales of this edition, starting
from the time the edition is published.

This edition of this book will be available under the aforementioned


Creative Commons license on July 1, 2012. Of course, watch the
CommonsWare Web site, as this edition might be relicensed sooner based
on sales.

For more details on the Creative Commons Attribution-Noncommercial-


Share Alike 3.0 license, visit the Creative Commons Web site.

Note that future editions of this book will become free on later dates, each
four years from the publication of that edition or based on sales of that
specific edition. Releasing one edition under the Creative Commons license
does not automatically release all editions under that license.

vi

SAMPLE EDITION - full edition at http://commonsware.com/Android/


PART I – Core Concepts

SAMPLE EDITION - full edition at http://commonsware.com/Android/


SAMPLE EDITION - full edition at http://commonsware.com/Android/
Welcome to the Book!

CHAPTER 1
The Big Picture

Android devices, by and large, will be mobile phones. While the Android
technology is being discussed for use in other areas (e.g., car dashboard
"PCs"), for the most part, you can think of Android as being used on phones.

For developers, this has benefits and drawbacks.

On the plus side, circa 2008, Android-style smartphones are sexy. Offering
Internet services over mobile devices dates back to the mid-1990's and the
Handheld Device Markup Language (HDML). However, only in recent years
have phones capable of Internet access taken off. Now, thanks to trends like
text messaging and to products like Apple's iPhone, phones that can serve as
Internet access devices are rapidly gaining popularity. So, working on
Android applications gives you experience with an interesting technology
(Android) in a fast-moving market segment (Internet-enabled phones),
which is always a good thing.

The problem comes when you actually have to program the darn things.

Anyone with experience in programming for PDAs or phones has felt the
pain of phones simply being small in all sorts of dimensions:

• Screens are small (you won't get comments like, "is that a 24-inch
LCD in your pocket, or...?")

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Welcome to the Book!

• Keyboards, if they exist, are small


• Pointing devices, if they exist, are annoying (as anyone who has lost
their stylus will tell you) or inexact (large fingers and "multi-touch"
LCDs are not a good mix)
• CPU speed and memory are tight compared to desktops and servers
you may be used to
• You can have any programming language and development
framework you want, so long as it was what the device manufacturer
chose and burned into the phone's silicon
• And so on

Moreover, applications running on a phone have to deal with the fact that
they're on a phone.

People with mobile phones tend to get very irritated when those phones
don't work, which is why the "can you hear me now?" ad campaign from
Verizon Wireless has been popular for the past few years. Similarly, those
same people will get irritated at you if your program "breaks" their phone:

• ...by tying up the CPU such that calls can't be received


• ...by not working properly with the rest of the phone's OS, such that
your application doesn't quietly fade to the background when a call
comes in or needs to be placed
• ...by crashing the phone's operating system, such as by leaking
memory like a sieve

Hence, developing programs for a phone is a different experience than


developing desktop applications, Web sites, or back-end server processes.
You wind up with different-looking tools, different-behaving frameworks,
and "different than you're used to" limitations on what you can do with your
program.

What Android tries to do is meet you halfway:

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Welcome to the Book!

• You get a commonly-used programming language (Java) with some


commonly used libraries (e.g., some Apache Commons APIs), with
support for tools you may be used to (Eclipse)
• You get a fairly rigid and uncommon framework in which your
programs need to run so they can be "good citizens" on the phone
and not interfere with other programs or the operation of the phone
itself

As you might expect, much of this book deals with that framework and how
you write programs that work within its confines and take advantage of its
capabilities.

What Androids Are Made Of

When you write a desktop application, you are "master of your own
domain". You launch your main window and any child windows – like dialog
boxes – that are needed. From your standpoint, you are your own world,
leveraging features supported by the operating system, but largely ignorant
of any other program that may be running on the computer at the same
time. If you do interact with other programs, it is typically through an API,
such as using JDBC (or frameworks atop it) to communicate with MySQL or
another database.

Android has similar concepts, but packaged differently, and structured to


make phones more crash-resistant.

Activities

The building block of the user interface is the activity. You can think of an
activity as being the Android analogue for the window or dialog in a desktop
application.

While it is possible for activities to not have a user interface, most likely your
"headless" code will be packaged in the form of content providers or
services, described below.

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Welcome to the Book!

Content Providers

Content providers provide a level of abstraction for any data stored on the
device that is accessible by multiple applications. The Android development
model encourages you to make your own data available to other
applications, as well as your own – building a content provider lets you do
that, while maintaining complete control over how your data gets accessed.

Intents

Intents are system messages, running around the inside of the device,
notifying applications of various events, from hardware state changes (e.g.,
an SD card was inserted), to incoming data (e.g., an SMS message arrived),
to application events (e.g., your activity was launched from the device's
main menu). Not only can you respond to intents, but you can create your
own, to launch other activities, or to let you know when specific situations
arise (e.g., raise such-and-so intent when the user gets within 100 meters of
this-and-such location).

Services

Activities, content providers, and intent receivers are all short-lived and can
be shut down at any time. Services, on the other hand, are designed to keep
running, if needed, independent of any activity. You might use a service for
checking for updates to an RSS feed, or to play back music even if the
controlling activity is no longer operating.

Stuff At Your Disposal

Storage

You can package data files with your application, for things that do not
change, such as icons or help files. You also can carve out a small bit of space
on the device itself, for databases or files containing user-entered or

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Welcome to the Book!

retrieved data needed by your application. And, if the user supplies bulk
storage, like an SD card, you can read and write files on there as needed.

Network

Android devices will generally be Internet-ready, through one


communications medium or another. You can take advantage of the Internet
access at any level you wish, from raw Java sockets all the way up to a built-in
WebKit-based Web browser widget you can embed in your application.

Multimedia

Android devices have the ability to play back and record audio and video.
While the specifics may vary from device to device, you can query the device
to learn its capabilities and then take advantage of the multimedia
capabilities as you see fit, whether that is to play back music, take pictures
with the camera, or use the microphone for audio note-taking.

GPS

Android devices will frequently have access to location providers, such as


GPS, that can tell your applications where the device is on the face of the
Earth. In turn, you can display maps or otherwise take advantage of the
location data, such as tracking a device's movements if the device has been
stolen.

Phone Services

And, of course, Android devices are typically phones, allowing your software
to initiate calls, send and receive SMS messages, and everything else you
expect from a modern bit of telephony technology.

SAMPLE EDITION - full edition at http://commonsware.com/Android/


SAMPLE EDITION - full edition at http://commonsware.com/Android/
CHAPTER 2
Project Structure

The Android build system is organized around a specific directory tree


structure for your Android project, much like any other Java project. The
specifics, though, are fairly unique to Android and what it all does to
prepare the actual application that will run on the device or emulator. Here's
a quick primer on the project structure, to help you make sense of it all,
particularly for the sample code referenced in this book.

Root Contents

...

The Sweat Off Your Brow

...

And Now, The Rest of the Story

...

What You Get Out Of It

...

SAMPLE EDITION - full edition at http://commonsware.com/Android/


SAMPLE EDITION - full edition at http://commonsware.com/Android/
CHAPTER 3
Inside the Manifest

The foundation for any Android application is the manifest file:


AndroidManifest.xml in the root of your project. Here is where you declare
what all is inside your application – the activities, the services, and so on.
You also indicate how these pieces attach themselves to the overall Android
system; for example, you indicate which activity (or activities) should appear
on the device's main menu (a.k.a., launcher).

When you create your application, you will get a starter manifest generated
for you. For a simple application, offering a single activity and nothing else,
the auto-generated manifest will probably work out fine, or perhaps require
a few minor modifications. On the other end of the spectrum, the manifest
file for the Android API demo suite is over 1,000 lines long. Your production
Android applications will probably fall somewhere in the middle.

Most of the interesting bits of the manifest will be described in greater


detail in the chapters on their associated Android features. For example, the
service element will be described in greater detail in the chapter on creating
services. For now, we just need to understand what the role of the manifest
is and its general overall construction.

In The Beginning, There Was the Root, And It


Was Good

...

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 3

Permissions, Instrumentations, and Applica-


tions (Oh, My!)

...

Your Application Does Something, Right?

...

10

SAMPLE EDITION - full edition at http://commonsware.com/Android/


PART II – Activities

SAMPLE EDITION - full edition at http://commonsware.com/Android/


SAMPLE EDITION - full edition at http://commonsware.com/Android/
CHAPTER 4
Creating a Skeleton Application

Every programming language or environment book starts off with the ever-
popular "Hello, World!" demonstration: just enough of a program to prove
you can build things, not so much that you cannot understand what is going
on. However, the typical "Hello, World!" program has no interactivity (e.g.,
just dumps the words to a console), and so is really boring.

This chapter demonstrates a simple project, but one using Advanced Push-
Button Technology™ and the current time, to show you how a simple
Android activity works.

Begin at the Beginning

To work with anything in Android, you need a project. With ordinary Java, if
you wanted, you could just write a program as a single file, compile it with
javac, and run it with java, without any other support structures. Android is
more complex, but to help keep it manageable, Google has supplied tools to
help create the project. If you are using an Android-enabled IDE, such as
Eclipse with the Android plugin, you can create a project inside of the IDE
(e.g., select File > New > Project, then choose Android > Android
Project).

If you are using tools that are not Android-enabled, you can use the
activityCreator.py script, found in the tools/ directory in your SDK
installation. Just pass activityCreator.py the package name of the activity

13

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 4

you want to create and a --out switch indicating where the project files
should be generated. For example:

./activityCreator.py --out /path/to/my/project/dir \


com.commonsware.android.Now

You will wind up with a handful of pre-generated files, as described in a


previous chapter.

For the purposes of the samples shown in this book, you can download their
project directories in a ZIP file on the CommonsWare Web site. These
projects are ready for use; you do not need to run activityCreator.py on
those unpacked samples.

The Activity

Your project's src/ directory contains the standard Java-style tree of


directories based upon the Java package you chose when you created the
project (e.g., com.commonsware.android results in
src/com/commonsware/android/). Inside the innermost directory you should
find a pre-generated source file named Now.java, which where your first
activity will go.

Open Now.java in your editor and paste in the following code:

package com.commonsware.android.skeleton;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Date;

public class Now extends Activity implements View.OnClickListener {


Button btn;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

btn = new Button(this);


btn.setOnClickListener(this);

14

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 4

updateTime();
setContentView(btn);
}

public void onClick(View view) {


updateTime();
}

private void updateTime() {


btn.setText(new Date().toString());
}
}

Or, if you download the source files off the Web site, you can just use the Now
project directly.

Dissecting the Activity

Let's examine this piece by piece:

package com.commonsware.android.skeleton;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Date;

The package declaration needs to be the same as the one you used when
creating the project. And, like any other Java project, you need to import any
classes you reference. Most of the Android-specific classes are in the android
package.

Remember that not every Java SE class is available to Android programs!


Visit the Android class reference to see what is and is not available.

public class Now extends Activity implements View.OnClickListener {


Button btn;

Activities are public classes, inheriting from the android.Activity base class.
In this case, the activity holds a button (btn). Since, for simplicity, we want

15

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 4

to trap all button clicks just within the activity itself, we also have the
activity class implement OnClickListener.

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

btn = new Button(this);


btn.setOnClickListener(this);
updateTime();
setContentView(btn);
}

The onCreate() method is invoked when the activity is started. The first
thing you should do is chain upward to the superclass, so the stock Android
activity initialization can be done.

In our implementation, we then create the button instance (new


Button(this)), tell it to send all button clicks to the activity instance itself
(via setOnClickListener()), call a private updateTime() method (see below),
and then set the activity's content view to be the button itself (via
setContentView()).

We will discuss that magical Bundle icicle in a later chapter. For the
moment, consider it an opaque handle that all activities receive upon
creation.

public void onClick(View view) {


updateTime();
}

In Swing, a JButton click raises an ActionEvent, which is passed to the


ActionListener configured for the button. In Android, a button click causes
onClick() to be invoked in the OnClickListener instance configured for the
button. The listener is provided the view that triggered the click (in this
case, the button). All we do here is call that private updateTime() method:

private void updateTime() {


btn.setText(new Date().toString());
}

16

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 4

When we open the activity (onCreate()) or when the button is clicked


(onClick()), we update the button's label to be the current time via
setText(), which functions much the same as the JButton equivalent.

Building and Running the Activity

To build the activity, either use your IDE's built-in Android packaging tool,
or run ant in the base directory of your project. Then, to run the activity:

• Launch the emulator (e.g., run tools/emulator from your Android


SDK installation)
• Install the package (e.g., run tools/adb install
/path/to/this/example/bin/Now.apk from your Android SDK
installation)
• View the list of installed applications in the emulator and find the
"Now" application

Figure 1. The Android application "launcher"

• Open that application

17

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 4

You should see an activity screen akin to:

Figure 2. The Now demonstration activity

Clicking the button – in other words, pretty much anywhere on the phone's
screen – will update the time shown in the button's label.

Note that the label is centered horizontally and vertically, as those are the
default styles applied to button captions. We can control that formatting,
which will be covered in a later chapter.

After you are done gazing at the awesomeness of Advanced Push-Button


Technology™, you can click the back button on the emulator to return to the
launcher.

18

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 5
Using XML-Based Layouts

While it is technically possible to create and attach widgets to our activity


purely through Java code, the way we did in the preceding chapter, the more
common approach is to use an XML-based layout file. Dynamic
instantiation of widgets is reserved for more complicated scenarios, where
the widgets are not known at compile-time (e.g., populating a column of
radio buttons based on data retrieved off the Internet).

With that in mind, it's time to break out the XML and learn out to lay out
Android activity views that way.

What Is an XML-Based Layout?

...

Why Use XML-Based Layouts?

...

OK, So What Does It Look Like?

...

19

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 5

What's With the @ Signs?

...

And We Attach These to the Java...How?


...

The Rest of the Story

...

20

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 6
Employing Basic Widgets

Every GUI toolkit has some basic widgets: fields, labels, buttons, etc.
Android's toolkit is no different in scope, and the basic widgets will provide
a good introduction as to how widgets work in Android activities.

Assigning Labels

...

Button, Button, Who's Got the Button?

...

Fleeting Images

...

Fields of Green. Or Other Colors.

...

21

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 6

Just Another Box to Check

...

Turn the Radio Up


...

It's Quite a View

...

Useful Properties

...

Useful Methods

...

22

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 7
Working with Containers

Containers pour a collection of widgets (and possibly child containers) into


specific layouts you like. If you want a form with labels on the left and fields
on the right, you will need a container. If you want OK and Cancel buttons
to be beneath the rest of the form, next to one another, and flush to right
side of the screen, you will need a container. Just from a pure XML
perspective, if you have multiple widgets (beyond RadioButton widgets in a
RadioGroup), you will need a container just to have a root element to place
the widgets inside.

Most GUI toolkits have some notion of layout management, frequently


organized into containers. In Java/Swing, for example, you have layout
managers like BoxLayout and containers that use them (e.g., Box). Some
toolkits stick strictly to the box model, such as XUL and Flex, figuring that
any desired layout can be achieved through the right combination of nested
boxes.

Android, through LinearLayout, also offers a "box" model, but in addition


supports a range of containers providing different layout rules. In this
chapter, we will look at three commonly-used containers: LinearLayout (the
box model), RelativeLayout (a rule-based model), and TableLayout (the grid
model), along with ScrollView, a container designed to assist with
implementing scrolling containers. In the next chapter, we will examine
some more esoteric containers.

23

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 7

Thinking Linearly

...

Concepts and Properties

...

Orientation

...

Fill Model

...

Weight

...

Gravity

...

Padding

...

Example

...

24

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 7

All Things Are Relative

...

Concepts and Properties

...

Positions Relative to Container

...

Relative Notation in Properties

...

Positions Relative to Other Widgets

...

Order of Evaluation

...

Example

...

Tabula Rasa

...

25

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 7

Concepts and Properties

...

Putting Cells in Rows

...

Non-Row Children of TableLayout

...

Stretch, Shrink, and Collapse

...

Example

...

Scrollwork

...

26

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 8
Using Selection Widgets

Back in the chapter on basic widgets, you saw how fields could have
constraints placed upon them to limit possible input, such as numeric-only
or phone-number-only. These sorts of constraints help users "get it right"
when entering information, particularly on a mobile device with cramped
keyboards.

Of course, the ultimate in constrained input is to select a choice from a set


of items, such as the radio buttons seen earlier. Classic UI toolkits have
listboxes, comboboxes, drop-down lists, and the like for that very purpose.
Android has many of the same sorts of widgets, plus others of particular
interest for mobile devices (e.g., the Gallery for examining saved photos).

Moreover, Android offers a flexible framework for determining what choices


are available in these widgets. Specifically, Android offers a framework of
data adapters that provide a common interface to selection lists ranging
from static arrays to database contents. Selection views – widgets for
presenting lists of choices – are handed an adapter to supply the actual
choices.

Adapting to the Circumstances

...

27

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 8

Using ArrayAdapter

...

Other Key Adapters

...

Lists of Naughty and Nice

...

Spin Control

...

Grid Your Lions (Or Something Like That...)

...

Fields: Now With 35% Less Typing!

...

Galleries, Give Or Take The Art

...

28

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 9
Employing Fancy Widgets and
Containers

The widgets and containers covered to date are not only found in many GUI
toolkits (in one form or fashion), but also are widely used in building GUI
applications, whether Web-based, desktop, or mobile. The widgets and
containers in this chapter are a little less widely used, though you will likely
find many to be quite useful.

Pick and Choose

...

Time Keeps Flowing Like a River

...

Making Progress

...

29

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 9

Putting It On My Tab

...

The Pieces

...

The Idiosyncrasies

...

Wiring It Together

...

Other Containers of Note

...

30

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 10
Applying Menus

Like applications for the desktop and some mobile operating systems, such
as PalmOS and Windows Mobile, Android supports activities with
"application" menus. Some Android phones will have a dedicated menu key
for popping up the menu; others will offer alternate means for triggering the
menu to appear.

Also, as with many GUI toolkits, you can create "context menus". On a
traditional GUI, this might be triggered by the right-mouse button. On
mobile devices, context menus typically appear when the user "taps-and-
holds" over a particular widget. For example, if a TextView had a context
menu, and the device was designed for finger-based touch input, you could
push the TextView with your finger, hold it for a second or two, and a pop-up
menu will appear for the user to choose from.

Where Android differs from most other GUI toolkits is in terms of menu
construction. While you can add items to the menu, you do not have full
control over the menu's contents, nor the timing of when the menu is built.
Part of the menu is system-defined, and that portion is managed by the
Android framework itself.

Flavors of Menu

...

31

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 10

Menus of Options

...

Menus in Context
...

Taking a Peek

...

32

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 11
Embedding the WebKit Browser

Other GUI toolkits let you use HTML for presenting information, from
limited HTML renderers (e.g., Java/Swing, wxWidgets) to embedding
Internet Explorer into .NET applications. Android is much the same, in that
you can embed the built-in Web browser as a widget in your own activities,
for displaying HTML or full-fledged browsing. The Android browser is
based on WebKit, the same engine that powers Apple's Safari Web browser.

The Android browser is sufficiently complex that it gets its own Java package
(android.webkit), though using the WebView widget itself can be simple or
powerful, based upon your requirements.

A Browser, Writ Small

...

Loading It Up

...

Navigating the Waters

...

33

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 11

Entertaining the Client

...

Settings, Preferences, and Options (Oh, My!)


...

34

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 12
Showing Pop-Up Messages

Sometimes, your activity (or other piece of Android code) will need to speak
up.

Not every interaction with Android users will be neat, tidy, and containable
in activities composed of views. Errors will crop up. Background tasks may
take way longer than expected. Something asynchronous may occur, such as
an incoming message. In these and other cases, you may need to
communicate with the user outside the bounds of the traditional user
interface.

Of course, this is nothing new. Error messages in the form of dialog boxes
have been around for a very long time. More subtle indicators also exist,
from task tray icons to bouncing dock icons to a vibrating cell phone.

Android has quite a few systems for letting you alert your users outside the
bounds of an Activity-based UI. One, notifications, is tied heavily into
intents and services and, as such, is covered in a later chapter. In this
chapter, you will see two means of raising pop-up messages: toasts and
alerts.

Raising Toasts

...

35

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 12

Alert! Alert!

...

Checking Them Out


...

36

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 13
Dealing with Threads

Ideally, you want your activities to be downright snappy, so your users don't
feel that your application is sluggish. Responding to user input quickly (e.g.,
200ms) is a fine goal. At minimum, though, you need to make sure you
respond within 5 seconds, lest the ActivityManager decide to play the role of
the Grim Reaper and kill off your activity as being non-responsive.

Of course, your activity might have real work to do, which takes non-
negligible amounts of time. There are two ways of dealing with this:

1. Do expensive operations in a background service, relying on


notifications to prompt users to go back to your activity
2. Do expensive work in a background thread

Android provides a veritable cornucopia of means to set up background


threads yet allow them to safely interact with the UI on the UI thread. These
include Handler objects, posting Runnable objects to the View, and using
UIThreadUtilities.

Getting Through the Handlers

...

37

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 13

Messages

...

Runnables

...

Running In Place

...

Utilities (And I Don't Mean Water Works)

...

And Now, The Caveats

...

38

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 14
Handling Activity Lifecycle
Events

While this may sound like a broken record...please remember that Android
devices, by and large, are phones. As such, some activities are more
important that others – taking a call is probably more important to users
than is playing Sudoku. And, since it is a phone, it probably has less RAM
than does your current desktop or notebook.

As a result, your activity may find itself being killed off because other
activities are going on and the system needs your activity's memory. Think
of it as the Android equivalent of the "circle of life" – your activity dies so
others may live, and so on. You cannot assume that your activity will run
until you think it is complete, or even until the user thinks it is complete.

This is one example – perhaps the most important example – of how an


activity's lifecycle will affect your own application logic. This chapter covers
the various states and callbacks that make up an activity's lifecycle and how
you can hook into them appropriately.

Schroedinger's Activity

...

39

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 14

Life, Death, and Your Activity

...

onCreate() and onCompleteThaw()

...

onStart(), onRestart(), and onResume()

...

onPause(), onFreeze(), onStop(), and onDestroy()

...

40

SAMPLE EDITION - full edition at http://commonsware.com/Android/


PART III – Data Stores, Network
Services, and APIs

SAMPLE EDITION - full edition at http://commonsware.com/Android/


SAMPLE EDITION - full edition at http://commonsware.com/Android/
CHAPTER 15
Using Preferences

Android has many different ways for you to store data for long-term use by
your activity. The simplest to use is the preferences system.

Android allows activities and applications to keep preferences, in the form


of key/value pairs (akin to a Map), that will hang around between invocations
of an activity. As the name suggests, the primary purpose is for you to store
user-specified configuration details, such as the last feed the user looked at
in your feed reader, or what sort order to use by default on a list, or whatever.
Of course, you can store in the preferences whatever you like, so long as it is
keyed by a String and has a primitive value (boolean, String, etc.)

Preferences can either be for a single activity or shared among all activities
in an application. Eventually, preferences might be shareable across
applications, but that is not supported as of the time of this writing.

Getting What You Want

...

Stating Your Preference

...

43

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 15

A Preference For Action

...

44

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 16
Accessing Files

While Android offers structured storage, via preferences and databases,


sometimes a simple file will suffice. Android offers two models for accessing
files: one for files pre-packaged with your application, and one for files
created on-device by your application.

You And The Horse You Rode In On

...

Readin' 'n Writin'

...

45

SAMPLE EDITION - full edition at http://commonsware.com/Android/


SAMPLE EDITION - full edition at http://commonsware.com/Android/
CHAPTER 17
Working with Resources

Resources are static bits of information held outside the Java source code.
You have seen one type of resource – the layout – frequently in the examples
in this book. There are many other types of resource, such as images and
strings, that you can take advantage of in your Android applications.

The Resource Lineup

...

String Theory

...

Plain Strings

...

String Formats

...

47

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 17

Styled Text

...

Styled Formats

...

Got the Picture?

...

XML: The Resource Way

...

Miscellaneous Values

...

Dimensions

...

Colors

...

Arrays

...

48

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 17

Different Strokes for Different Folks

...

49

SAMPLE EDITION - full edition at http://commonsware.com/Android/


SAMPLE EDITION - full edition at http://commonsware.com/Android/
CHAPTER 18
Managing and Accessing Local
Databases

SQLite is a very popular embedded database, as it combines a clean SQL


interface with a very small memory footprint and decent speed. Moreover, it
is public domain, so everyone can use it. Lots of firms (Adobe, Apple,
Google, Sun, Symbian) and open source projects (Mozilla, PHP, Python) all
ship products with SQLite.

For Android, SQLite is "baked into" the Android runtime, so every Android
application can create SQLite databases. Since SQLite uses a SQL interface,
it is fairly straightforward to use for people with experience in other SQL-
based databases. However, its native API is not JDBC, and JDBC might be
too much overhead for a memory-limited device like a phone, anyway.
Hence, Android programmers have a different API to learn – the good news
being is that it is not that difficult.

This chapter will cover the basics of SQLite use in the context of working on
Android. It by no means is a thorough coverage of SQLite as a whole. If you
want to learn more about SQLite and how to use it in other environment
than Android, a fine book is The Definitive Guide to SQLite by Michael
Owens.

Activities will typically access a database via a content provider or service. As


such, this chapter does not have a full example. You will find a full example

51

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 18

of a content provider that accesses a database in the Building a Content


Provider chapter.

A Quick SQLite Primer

...

Start at the Beginning

...

Setting the Table

...

Makin' Data

...

What Goes Around, Comes Around

...

Raw Queries

...

Regular Queries

...

52

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 18

Building with Builders

...

Using Cursors

...

Change for the Sake of Change

...

Making Your Own Cursors

...

Data, Data, Everywhere

...

53

SAMPLE EDITION - full edition at http://commonsware.com/Android/


SAMPLE EDITION - full edition at http://commonsware.com/Android/
CHAPTER 19
Leveraging Java Libraries

Java has as many, if not more, third-party libraries than any other modern
programming language. Here, "third-party libraries" refer to the
innumerable JARs that you can include in a server or desktop Java
application – the things that the Java SDKs themselves do not provide.

In the case of Android, the Dalvik VM at its heart is not precisely Java, and
what it provides in its SDK is not precisely the same as any traditional Java
SDK. That being said, many Java third-party libraries still provide
capabilities that Android lacks natively and therefore may be of use to you
in your project, for the ones you can get working with Android's flavor of
Java.

This chapter explains what it will take for you to leverage such libraries and
the limitations on Android's support for arbitrary third-party code.

The Outer Limits

...

Ants and Jars

...

55

SAMPLE EDITION - full edition at http://commonsware.com/Android/


SAMPLE EDITION - full edition at http://commonsware.com/Android/
CHAPTER 20
Communicating via the Internet

The expectation is that most, if not all, Android devices will have built-in
Internet access. That could be WiFi, cellular data services (EDGE, 3G, etc.),
or possibly something else entirely. Regardless, most people – or at least
those with a data plan or WiFi access – will be able to get to the Internet
from their Android phone.

Not surprisingly, the Android platform gives developers a wide range of ways
to make use of this Internet access. Some offer high-level access, such as the
integrated WebKit browser component we saw in an earlier chapter. If you
want, you can drop all the way down to using raw sockets. Or, in between,
you can leverage APIs – both on-device and from 3rd-party JARs – that give
you access to specific protocols: HTTP, XMPP, SMTP, and so on.

The emphasis of this book is on the higher-level forms of access: the WebKit
component and Internet-access APIs, as busy coders should be trying to
reuse existing components versus rolling one's own on-the-wire protocol
wherever possible.

REST and Relaxation

...

57

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 20

HTTP Operations via Apache Commons

...

Parsing Responses

...

Stuff To Consider

...

Email over Java

...

58

SAMPLE EDITION - full edition at http://commonsware.com/Android/


PART IV – Intents

SAMPLE EDITION - full edition at http://commonsware.com/Android/


SAMPLE EDITION - full edition at http://commonsware.com/Android/
CHAPTER 21
Creating Intent Filters

Up to now, the focus of this book has been on activities opened directly by
the user from the device's launcher. This, of course, is the most obvious case
for getting your activity up and visible to the user. And, in many cases it is
the primary way the user will start using your application.

However, remember that the Android system is based upon lots of loosely-
coupled components. What you might accomplish in a desktop GUI via
dialog boxes, child windows, and the like are mostly supposed to be
independent activities. While one activity will be "special", in that it shows
up in the launcher, the other activities all need to be reached...somehow.

The "how" is via intents.

An intent is basically a message that you pass to Android saying, "Yo! I want
to do...er...something! Yeah!" How specific the "something" is depends on
the situation – sometimes you know exactly what you want to do (e.g., open
up one of your other activities), and sometimes you don't.

In the abstract, Android is all about intents and receivers of those intents.
So, now that we are well-versed in creating activities, let's dive into intents,
so we can create more complex applications while simultaneously being
"good Android citizens".

61

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 21

What's Your Intent?

...

Pieces of Intents

...

Stock Options

...

Intent Routing

...

Stating Your Intent(ions)

...

Narrow Receivers

...

62

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 22
Launching Activities and Sub-
Activities

As discussed previously, the theory behind the Android UI architecture is


that developers should decompose their application into distinct activities,
each implemented as an Activity, each reachable via intents, with one
"main" activity being the one launched by the Android launcher. For
example, a calendar application could have activities for viewing the
calendar, viewing a single event, editing an event (including adding a new
one), and so forth.

This, of course, implies that one of your activities has the means to start up
another activity. For example, if somebody clicks on an event from the view-
calendar activity, you might want to show the view-event activity for that
event. This means that, somehow, you need to be able to cause the view-
event activity to launch and show a specific event (the one the user clicked
upon).

This can be further broken down into two scenarios:

1. You know what activity you want to launch, probably because it is


another activity in your own application
2. You have a content Uri to...something, and you want your users to be
able to do...something with it, but you do not know up front what
the options are

63

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 22

This chapter covers the first scenario; the next chapter handles the second.

Peers and Subs

...

Start 'Em Up

...

Make an Intent

...

Make the Call

...

64

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 23
Finding Available Actions via
Introspection

Sometimes, you know just what you want to do, such as display one of your
other activities.

Sometimes, you have a pretty good idea of what you want to do, such as view
the content represented by a Uri, or have the user pick a piece of content of
some MIME type.

Sometimes, you're lost. All you have is a content Uri, and you don't really
know what you can do with it.

For example, suppose you were creating a common tagging subsystem for
Android, where users could tag pieces of content – contacts, Web URLs,
geographic locations, etc. Your subsystem would hold onto the Uri of the
content plus the associated tags, so other subsystems could, say, ask for all
pieces of content referencing some tag.

That's all well and good. However, you probably need some sort of
maintenance activity, where users could view all their tags and the pieces of
content so tagged. This might even serve as a quasi-bookmark service for
items on their phone. The problem is, the user is going to expect to be able
to do useful things with the content they find in your subsystem, such as
dial a contact or show a map for a location.

65

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 23

The problem is, you have absolutely no idea what is all possible with any
given content Uri. You probably can view any of them, but can you edit
them? Can you dial them? Since new applications with new types of content
could be added by any user at any time, you can't even assume you know all
possible combinations just by looking at the stock applications shipped on
all Android devices.

Fortunately, the Android developers thought of this.

Android offers various means by which you can present to your users a set of
likely activities to spawn for a given content Uri...even if you have no idea
what that content Uri really represents. This chapter explores some of these
Uri action introspection tools.

Pick 'Em

...

Adaptable Adapters

...

Would You Like to See the Menu?

...

Asking Around

...

66

SAMPLE EDITION - full edition at http://commonsware.com/Android/


PART V – Content Providers and
Services

SAMPLE EDITION - full edition at http://commonsware.com/Android/


SAMPLE EDITION - full edition at http://commonsware.com/Android/
CHAPTER 24
Using a Content Provider

Any Uri in Android that begins with the content:// scheme represents a
resource served up by a content provider. Content providers offer data
encapsulation using Uri instances as handles – you neither know nor care
where the data represented by the Uri comes from, so long as it is available
to you when needed. The data could be stored in a SQLite database, or in
flat files, or retrieved off a device, or be stored on some far-off server
accessed over the Internet.

Given a Uri, you can perform basic CRUD (create, read, update, delete)
operations using a content provider. Uri instances can represent either
collections or individual pieces of content. Given a collection Uri, you can
create new pieces of content via insert operations. Given an instance Uri,
you can read data represented by the Uri, update that data, or delete the
instance outright.

Android lets you use existing content providers, plus create your own. This
chapter covers using content providers; the next chapter will explain how
you can serve up your own data using the content provider framework.

Pieces of Me

...

69

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 24

Getting a Handle

...

Makin' Queries
...

Adapting to the Circumstances

...

Doing It By Hand

...

Position

...

Getting Properties

...

Setting Properties

...

Give and Take

...

70

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 24

Beware of the BLOB!

...

71

SAMPLE EDITION - full edition at http://commonsware.com/Android/


SAMPLE EDITION - full edition at http://commonsware.com/Android/
CHAPTER 25
Building a Content Provider

Building a content provider is probably the most complicated and tedious


task in all of Android development. There are many requirements of a
content provider, in terms of methods to implement and public data
members to supply. And, until you try using it, you have no great way of
telling if you did any of it correctly (versus, say, building an activity and
getting validation errors from the resource compiler).

That being said, building a content provider is of huge importance if your


application wishes to make data available to other applications. If your
application is keeping its data solely to itself, you may be able to avoid
creating a content provider, just accessing the data directly from your
activities. But, if you want your data to possibly be used by others – for
example, you are building a feed reader and you want other programs to be
able to access the feeds you are downloading and caching – then a content
provider is right for you.

First, Some Dissection

...

Next, Some Typing

...

73

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 25

Step #1: Create a Provider Class

...

ContentProvider

...

onCreate()

...

query()

...

insert()

...

update()

...

delete()

...

getType()

...

74

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 25

DatabaseContentProvider

...

Step #2: Supply a Uri

...

Step #3: Declare the Properties

...

Step #4: Update the Manifest

...

Notify-On-Change Support

...

75

SAMPLE EDITION - full edition at http://commonsware.com/Android/


SAMPLE EDITION - full edition at http://commonsware.com/Android/
CHAPTER 26
Requesting and Requiring
Permissions

In the late 1990's, a wave of viruses spread through the Internet, delivered
via email, using contact information culled from Microsoft Outlook. A virus
would simply email copies of itself to each of the Outlook contacts that had
an email address. This was possible because, at the time, Outlook did not
take any steps to protect data from programs using the Outlook API, since
that API was designed for ordinary developers, not virus authors.

Nowadays, many applications that hold onto contact data secure that data
by requiring that a user explicitly grant rights for other programs to access
the contact information. Those rights could be granted on a case-by-case
basis or a once at install time.

Android is no different, in that it requires permissions for applications to


read or write contact data. Android's permission system is useful well
beyond contact data, and for content providers and services beyond those
supplied by the Android framework.

You, as an Android developer, will frequently need to ensure your


applications have the appropriate permissions to do what you want to do
with other applications' data. You may also elect to require permissions for
other applications to use your data or services, if you make those available to
other Android components. This chapter covers how to accomplish both
these ends.

77

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 26

Mother, May I?

...

Halt! Who Goes There?


...

Enforcing Permissions via the Manifest

...

Enforcing Permissions Elsewhere

...

May I See Your Documents?

...

78

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 27
Creating a Service

As noted previously, Android services are for long-running processes that


may need to keep running even when decoupled from any activity. Examples
include playing music even if the "player" activity gets garbage-collected,
polling the Internet for RSS/Atom feed updates, and maintaining an online
chat connection even if the chat client loses focus due to an incoming phone
call.

Services are created when manually started (via an API call) or when some
activity tries connecting to the service via inter-process communication
(IPC). Services will live until no longer needed and if RAM needs to be
reclaimed. Running for a long time isn't without its costs, though, so
services need to be careful not to use too much CPU or keep radios active
too much of the time, lest the service cause the device's battery to get used
up too quickly.

This chapter covers how you can create your own services; the next chapter
covers how you can use such services from your activities or other contexts.
Both chapters will analyze the MailBuzz sample application (MailBuzz), with
this chapter focusing mostly on the MailBuzzService implementation.
MailBuzzService polls a supplied email account, either on-demand or on a
stated interval, to see if new messages have arrived, at which it will post a
Notification (as described in the chapter on notifications).

79

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 27

Getting Buzzed

...

Service with Class


...

When IPC Attacks!

...

Write the AIDL

...

Implement the Interface

...

Manifest Destiny

...

Where's the Remote?

...

80

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 28
Invoking a Service

Services can be used by any application component that "hangs around" for
a reasonable period of time. This includes activities, content providers, and
other services. Notably, it does not include pure intent receivers (i.e., intent
receivers that are not part of an activity), since those will get garbage
collected immediately after each instance processes one incoming Intent.

To use a service, you need to get an instance of the AIDL interface for the
service, then call methods on that interface as if it were a local object. When
done, you can release the interface, indicating you no longer need the
service.

In this chapter, we will look at the client side of the MailBuzz sample
application (MailBuzz). The MailBuzz activity provides fields for the account
information (server type, server, etc.), a checkbox to toggle whether polling
for new mail should go on, a button to push the account information to the
service, and another button to check right now for new messages.

When run, the activity looks like this:

81

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 28

Figure 3. The MailBuzz service client

Bound for Success

...

Request for Service

...

Prometheus Unbound

...

Manual Transmission

...

82

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 29
Alerting Users Via Notifications

Pop-up messages. Tray icons and their associated "bubble" messages.


Bouncing dock icons. You are no doubt used to programs trying to get your
attention, sometimes for good reason.

Your phone also probably chirps at you for more than just incoming calls:
low battery, alarm clocks, appointment notifications, incoming text message
or email, etc.

Not surprisingly, Android has a whole framework for dealing with these
sorts of things, collectively called "notifications".

Types of Pestering

...

Hardware Notifications

...

Icons

...

83

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 29

Letting Your Presence Be Felt

...

84

SAMPLE EDITION - full edition at http://commonsware.com/Android/


PART VI – Other Android Capabilities

SAMPLE EDITION - full edition at http://commonsware.com/Android/


SAMPLE EDITION - full edition at http://commonsware.com/Android/
CHAPTER 30
Accessing Location-Based
Services

A popular feature on current-era mobile devices is GPS capability, so the


device can tell you where you are at any point in time. While the most
popular use of GPS service is mapping and directions, there are other things
you can do if you know your location. For example, you might set up a
dynamic chat application where the people you can chat with are based on
physical location, so you're chatting with those you are nearest. Or, you
could automatically "geotag" posts to Twitter or similar services.

GPS is not the only way a mobile device can identify your location.
Alternatives include:

• The European equivalent to GPS, called Galileo, which is still under


development at the time of this writing
• Cell tower triangulation, where your position is determined based
on signal strength to nearby cell towers
• Proximity to public WiFi "hotspots" that have known geographic
locations

Android devices may have one or more of these services available to them.
You, as a developer, can ask the device for your location, plus details on what
providers are available. There are even ways for you to simulate your location
in the emulator, for use in testing your location-enabled applications.

87

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 30

Location Providers: They Know Where You're


Hiding

...

Finding Yourself

...

On the Move

...

Are We There Yet? Are We There Yet? Are We


There Yet?

...

Testing...Testing...

...

88

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 31
Mapping with MapView and
MapActivity

One of Google's most popular services – after search, of course – is Google


Maps, where you can find everything from the nearest pizza parlor to
directions from New York City to San Francisco (only 2,905 miles!) to street
views and satellite imagery.

Android, not surprisingly, integrates Google Maps. There is a mapping


activity available to users straight off the main Android launcher. More
relevant to you, as a developer, are MapView and MapActivity, which allow you
to integrate maps into your own applications. Not only can you display
maps, control the zoom level, and allow people to pan around, but you can
tie in Android's location-based services to show where the device is and
where it is going.

Fortunately, integrating basic mapping features into your Android project is


fairly easy. However, there is a fair bit of power available to you, if you want
to get fancy.

The Bare Bones

...

89

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 31

Exercising Your Control

...

Zoom

...

Center

...

Reticle

...

Traffic and Terrain

...

Follow You, Follow Me

...

Layers Upon Layers

...

Overlay Classes

...

90

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 31

Drawing the Overlay

...

Handling Screen Taps

...

91

SAMPLE EDITION - full edition at http://commonsware.com/Android/


SAMPLE EDITION - full edition at http://commonsware.com/Android/
CHAPTER 32
Playing Media

Pretty much every phone claiming to be a "smartphone" has the ability to at


least play back music, if not video. Even many more ordinary phones are
full-fledged MP3 players, in addition to offering ringtones and whatnot.

Not surprisingly, Android aims to match the best of them.

Android has full capability to play back and record audio and video. This
includes:

• Playback of audio, such as downloaded MP3 tracks


• Showing photos
• Playing back video clips
• Voice recording through the microphone
• Camera for still pictures or video clips

Exactly how robust these capabilities will be is heavily device-dependent.


Mobile device cameras range from excellent to atrocious. Screen resolutions
and sizes will vary, and video playback works better on better screens.
Which codecs a device manufacturer will license (e.g., what types of video
can it play?) and which Bluetooth profiles a device will support (e.g., A2DP
for stereo?) will also have an impact on what results any given person will
have with their phone.

93

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 32

You as a developer can integrate media playback and recording into your
applications. Recording is outside the scope of this book, in large part
because the current emulator has recording limitations at this time. And,
viewing pictures is mostly a matter of putting an ImageView widget into an
activity. This chapter, therefore, focuses on playback of audio and video.

As with many advanced Android features, expect changes in future releases


of their toolkit. For example, at the time of this writing, there is no built-in
audio or video playback activity. Hence, you cannot just craft an Intent to,
say, an MP3 URL, and hand it off to Android with VIEW_ACTION to initiate
playback. Right now, you need to handle the playback yourself. It is probably
safe to assume, though, that standard activities for this will be forthcoming,
allowing you to "take the easy way out" if you want to play back media but
do not need to control that playback much yourself.

Get Your Media On

...

Making Noise

...

Moving Pictures

...

94

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 33
Handling Telephone Calls

Many, if not most, Android devices will be phones. As such, not only will
users be expecting to place and receive calls using Android, but you will
have the opportunity to help them place calls, if you wish.

Why might you want to?

• Maybe you are writing an Android interface to a sales management


application (a la Salesforce.com) and you want to offer users the
ability to call prospects with a single button click, and without them
having to keep those contacts both in your application and in the
phone's contacts application
• Maybe you are writing a social networking application, and the
roster of phone numbers that you can access shifts constantly, so
rather than try to "sync" the social network contacts with the
phone's contact database, you let people place calls directly from
your application
• Maybe you are creating an alternative interface to the existing
contacts system, perhaps for users with reduced motor control (e.g.,
the elderly), sporting big buttons and the like to make it easier for
them to place calls

Whatever the reason, Android has APIs to let you manipulate the phone just
like any other piece of the Android system.

95

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 33

No, No, No – Not That IPhone...

...

What's Our Status?


...

You Make the Call!

...

96

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 34
Searching with SearchManager

One of the firms behind the Open Handset Alliance – Google – has a teeny
weeny Web search service, one you might have heard of in passing. Given
that, it's not surprising that Android has some amount of built-in search
capabilities.

Specifically, Android has "baked in" the notion of searching not only on the
device for data, but over the air to Internet sources of data.

Your applications can participate in the search process, by triggering


searches or perhaps by allowing your application's data to be searched.

Note that this is fairly new to the Android platform, and so some shifting in
the APIs is likely. Stay tuned for updates to this chapter.

Hunting Season

...

Search Yourself

...

97

SAMPLE EDITION - full edition at http://commonsware.com/Android/


CHAPTER 34

Craft the Search Activity

...

Update the Manifest

...

Try It Out

...

98

SAMPLE EDITION - full edition at http://commonsware.com/Android/


PART VII – Appendices

SAMPLE EDITION - full edition at http://commonsware.com/Android/


SAMPLE EDITION - full edition at http://commonsware.com/Android/
APPENDIX A
The TourIt Sample Application

In several chapters of this book, we used TourIt as a source of sample code


for features ranging from content providers to mapping and location
services. This appendix discusses the application as a whole, so you can see
all facets of it from front to back.

Installing TourIt

...

Demo Location Provider

...

SD Card Image with Sample Tour

...

Running TourIt

...

101

SAMPLE EDITION - full edition at http://commonsware.com/Android/


APPENDIX A

Main Activity

...

Configuration Activity

...

Cue Sheet Activity

...

Map Activity

...

Tour Update Activity

...

Help Activity

...

TourIt's Manifest

...

TourIt's Content

...

102

SAMPLE EDITION - full edition at http://commonsware.com/Android/


APPENDIX A

Data Storage

...

Content Provider

...

Model Classes

...

TourIt's Activities

...

TourListActivity

...

TourViewActivity

...

Custom List Contents

...

Details Panel

...

103

SAMPLE EDITION - full edition at http://commonsware.com/Android/


APPENDIX A

TourMapActivity

...

TourEditActivity

...

HelpActivity

...

ConfigActivity

...

104

SAMPLE EDITION - full edition at http://commonsware.com/Android/


The TourIt Sample Application

String page=getIntent().getStringExtra(PAGE);

if (page==null) {
browser.loadUrl("file:///android_asset/index.html");
}
else {
browser.loadUrl("file:///android_asset/"+page+".html");
}
}

By default, it will load the home page. If, however, the activity was started by
another activity that passed in a specific page to view, it loads that page
instead.

HelpActivity hooks into the WebKit browser to detect clicks on links. Since
the only links in the help are to other help pages, it simply loads in the
requested page:

private class Callback extends WebViewClient {


public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);

return(true);
}
}

ConfigActivity

The ConfigActivity class mostly loads data out of preferences, updates the
layout's widgets to match, then reverses the process when the activity is
paused (e.g., when the user clicks Close from the options menu).

The most interesting thing here is the spinner of location providers – this is
covered in detail in the chapter on location services.

368

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Keyword Index

Class BoxLayout.................................................................41

Builder..............................................................118, 119
AbsoluteLayout.......................................................96
Bundle................................133, 134, 201, 210, 264, 277
ActionEvent.............................................................20
Button...........................23, 25-28, 30, 31, 155, 159, 322
ActionListener.........................................................20
Calendar..................................................................86
Activity. .8, 68, 101, 117, 118, 126, 128, 132, 137, 147, 173,
174, 206, 207
Canvas............................................................308, 309
ActivityAdapter................................68, 220, 223, 225
CharSequence.......................................................268
ActivityIconAdapter........................................68, 220
CheckBox......................................................34, 37, 39
ActivityManager.....................................................123
Chrono.....................................................................84
Adapter..................................................................220
Clocks......................................................................88
AdapterView...........................................................101
ComponentName...................................224, 225, 275
AlertDialog.......................................................118, 119
CompoundButton...................................................37
AnalogClock............................................................88
ConcurrentLinkedQueue......................................265
ArrayAdapter.................66, 67, 69, 77, 146, 289, 364
ConfigActivity.................282, 289, 291, 305, 362, 368
ArrayList.................................................................146
ContentManager...................................................280
AudioDemo............................................................316
ContentObserver...........................................254, 255
AutoComplete.........................................................79
ContentProvider................173, 174, 177, 238, 239, 243
AutoCompleteTextView..............................33, 78-80
ContentResolver.....................................237, 239, 254
BaseColumns.........................................................252
ContentValues..................175, 237, 238, 247, 249, 253
Box............................................................................41
Context..........................66, 118, 137, 147, 173, 174, 234

369

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Keyword Index

ContextMenu...................................................100, 101 Grid..........................................................................75

ContextMenuInfo...................................................101 GridView......................................................74, 75, 82

Criteria...................................................290, 292, 306 Handler......................................123-128, 133, 295, 296

Critieria..........................................................290, 297 HelpActivity...................................209, 362, 367, 368

Cursor....67, 179, 180, 231, 233-237, 239, 246, 247, 253 HttpClient.......................................................188-190

CursorAdapter.........................................................67 HttpMethod...........................................................188

CursorFactory..................................................173, 180 IBinder...........................................................266, 275

DatabaseContentProvider.....................243, 244, 252 ImageButton..............................................31, 158, 159

DatabaseHelper.....................................................244 Images.....................................................................158

DateFormat.............................................................86 ImageView..........................................31, 158, 240, 314

DatePicker...............................................................83 InputMethod...........................................................32

DatePickerDialog..............................................83, 86 InputStream...............................143, 146, 147, 188, 191

DeadObjectException...................................276, 329 InputStreamReader................................................147

Dialer......................................................................327 Intent......90, 111, 112, 220, 223-225, 243, 273, 275, 277,
281, 292-295, 305, 314, 339
Dialog.....................................................................128
IntentReceiver...............................................205, 206
DigitalClock............................................................88
IPhone....................................................326, 328, 329
Direction.........................................................237, 361
Iterator...................................................................235
Document..............................................................146
JButton................................................................20, 21
Double...................................................................209
JCheckBox...............................................................66
Drawable..............................................82, 93, 158, 281
JComboBox..............................................................70
EditView.....................................31, 32, 78, 79, 83, 231
JLabel.......................................................................66
ExpandableListView...............................................96
JList..........................................................................66
Field.........................................................................32
JTabbedPane...........................................................90
FloatInputMethod.................................................367
JTable.......................................................................66
FlowLayout..............................................................42
Label........................................................................30
Folder......................................................................195
Launch....................................................................210
Forecast...................................................................191
Linear.......................................................................45
FrameLayout.......................................................91-93
LinearLayout...........................................41-46, 58, 93
Gallery................................................................65, 82
List............................................68, 102, 225, 268, 289
GetMethod......................................................188, 190
ListActivity..........................................68, 69, 92, 299

370

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Keyword Index

ListAdapter..............................................96, 220, 339 NowRedux................................................................27

ListCellRenderer.....................................................66 Object......................................................................101

ListDemo................................................................104 OnCheckedChangeListener................34, 35, 48, 140

ListView. . .68, 70, 71, 82, 102, 188, 220, 233, 234, 299, OnClickListener............................20, 86, 121, 140, 211
336, 339, 363
OnCompletionListener.........................................319
Location.........................................................189, 290
OnDateSetListener...........................................84, 86
LocationIntentReceiver........................................306
OnItemSelectedListener.........................................72
LocationManager...................288-290, 292, 293, 305
OnPopulateContextMenuListener.................101, 103
LocationProvider...................288-290, 292, 296, 297
OnPreparedListener..............................................319
Lorem.....................................................................336
OnTimeSetListener..........................................84, 86
LoremBase..............................................................337
OutputStream........................................................147
LoremDemo....................................................339, 341
OutputStreamWriter.............................................147
LoremSearch..........................................................341
Overlay...........................................................308, 310
MailBuzz...........................185, 193, 263, 265, 273, 274
OverlayController.................................................308
MailBuzzService....................................263, 264, 267
PackageManager....................................................225
MailClient..............................................................194
Parcelable..............................................................268
Map............................................134, 137, 175, 237, 268
Pick.................................................................216, 258
MapActivity....................................................299-301
PickDemo..............................................................258
MapController........................................301, 302, 305
PixelCalculator................................................308-310
MapView........................................................299-304
PixelConverter.......................................................309
MediaController............................................322, 324
Point..................................................302, 303, 309-311
MediaPlayer......................................315, 316, 319, 320
PostMethod............................................................188
Menu.........................................................98, 100, 223
Prefs........................................................................139
Menu.Item.................................................99-101, 225
ProgressBar........................................90, 125, 126, 129
Menus.....................................................................102
Provider....................................233, 244, 247-252, 361
Message..............................................119, 124, 126, 127
ProviderWrapper..................................................289
MessageCountListener..........................................194
ProximityIntentReceiver.......................................294
MyActivity.............................................................224
RadioButton..................................................37-41, 46
Notification............................................263, 280, 281
RadioGroup..........................37, 38, 40, 41, 46, 48, 49
NotificationManager.....................................280, 281
ReadWrite...............................................................147
Now...............................................................19, 27, 28

371

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Keyword Index

RectF........................................................................311 TableRow............................................................56-58

Relative.....................................................................53 TabSpec..............................................................93, 94

RelativeLayout.............................41, 50, 51, 54, 55, 59 TabWidget..........................................................91-93

Resources.........................................................143, 161 TextView.....26, 29-31, 34, 37, 67, 77, 86, 97, 234, 336

RouteAdapter........................................................364 TextWatcher......................................................79, 80

RouteOverlay..........................................308, 309, 311 TimePicker........................................................83, 84

Runnable..............................99, 100, 123, 124, 127, 128 TimePickerDialog.......................................83, 84, 86

Scroll........................................................................60 TimerTask..............................................................266

ScrollView....................................................41, 60, 62 Toast..............................................117, 118, 121, 190, 311

SecretsProvider......................................................242 Tour...........................................232, 233, 236, 237, 361

SecurityException.................................................258 TourEditActivity.....................238, 291, 360, 362, 367

Service...........................................................264, 269 TourIt.....................................................................300

ServiceConnection.........................................274-276 TourListActivity..............................232, 234, 360, 362

Session....................................................................195 TourMapActivity.....300, 304, 305, 307, 308, 362, 367

SharedPreferences..................................................138 TourViewActivity. . .232, 233, 282, 283, 293, 294, 360,


362, 363, 366
SimpleAdapter........................................................67
UIThreadUtilities............................................123, 128
SimpleCursorAdapter.............................233-235, 363
Uri..........31, 158, 178, 200, 201, 203-205, 207, 209, 211,
Spanned...........................................................153, 154 215-217, 220, 224, 229-232, 237-243, 246-252, 254,
255, 280, 314, 315
Spinner....................................70, 71, 78, 82, 220, 233
VideoDemo............................................................323
SQLiteDatabase...............................................173-175
VideoView.......................................................321-324
SQLiteQueryBuilder........................176-178, 246, 247
View..........23, 26, 27, 39, 58, 62, 101, 118, 123, 127, 128
Static................................................................144, 161
ViewFlipper............................................362, 365, 366
Store........................................................................195
Waypoint.........................................................237, 361
String99, 118, 119, 137, 154, 156, 188, 210, 231, 246, 268
Weather..................................................................188
Strings.....................................................................154
WeatherDemo.......................................................190
Tab...........................................................................92
WebKit............................................................188, 190
TabActivity.................................................92, 94, 362
WebSettings............................................................114
TabHost..............................................................91-94
WebView..............................................107-109, 111-115
Table........................................................................59
WebViewClient.................................................112, 113
TableLayout..................................................41, 56-59
XmlPullParser..................................................161, 162

372

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Keyword Index

Command END_TAG................................................................161

FACTORY_TEST_ACTION....................................201
adb pull...................................................................181
FIRST.......................................................................99
adb push..................................................181, 323, 348
GADGET_CATEGORY..........................................202
adb shell.........................................................180, 348
GET.........................................................................188
ant..........................................................................8, 9
GET_CONTENT_ACTION....................................201
ant install...............................................................347
HOME_CATEGORY..............................................202
dex...................................................................185, 186
HORIZONTAL........................................................42
sqlite3.....................................................................180
ID............................................................................174

Constant INBOX....................................................................195

ACCESS_ASSISTED_GPS......................................288 INSERT.....................................................172, 175, 176

ACCESS_CELL_ID.................................................288 INSERT_ACTION..................................................201

ACCESS_GPS.........................................................288 INTEGER................................................................172

ACCESS_POSITION.............................................288 LARGER...................................................................115

ALTERNATE_CATEGORY....................................223 LAUNCHER...................................................201, 204

ALTERNATIVE...............................................201, 224 LAUNCHER_CATEGORY.....................................202

ALTERNATIVE_CATEGORY........................202, 224 LENGTH_LONG.....................................................118

ANSWER_ACTION...............................................201 LENGTH_SHORT...................................................118

BIND_AUTO_CREATE.........................................275 MAIN.....................................................................204

BROWSABLE_CATEGORY...................................202 MAIN_ACTION.............................................201, 202

CALL_ACTION......................................................201 MATCH_DEFAULT_ONLY...................................225

CONTENT_URI.....................................................254 MEDIA_MOUNTED_ACTION............................202

DEFAULT................................................................201 NULL.......................................................................175

DEFAULT_CATEGORY..................................202, 225 ORDER BY..............................................................231

DELETE....................................................175, 176, 239 PERMISSION_DENIED.........................................261

DELETE_ACTION.................................................201 PERMISSION_GRANTED.....................................261

DIAL_ACTION.......................................................201 PICK_ACTION.........................200-202, 210, 231, 243

DIALER_DEFAULT_KEYS.....................................335 PICK_ACTIVITY_ACTION.....................201, 216, 217

EDIT_ACTION...............................................200-202 POLL..............................................................266, 269

END_DOCUMENT................................................161 POST......................................................................188

373

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Keyword Index

PREFERENCE_CATEGORY..................................202 add()..................................................................98, 99

PROJECTION.........................................................232 addId()...................................................................230

R...............................................................................27 addIntentOptions()................................100, 223-225

READ_CONTACTS................................................258 addMenu().............................................................100

RECEIVE_SMS.......................................................262 addProximityAlert()..............................................293

RESULT_CANCELLED..........................................210 addSeparator().......................................................100

RESULT_FIRST_USER...........................................210 addSubMenu().......................................................100

RESULT_OK.............................................210, 216, 217 addTab()..................................................................94

RUN_ACTION.......................................................201 appendWhere()......................................................178

SEARCH_ACTION.........................................201, 339 applyFormat()........................................................156

SELECT..............................................172, 176, 178, 179 applyMenuChoice()...............................................104

SELECTED_ALTERNATIVE_CATEGORY............202 beforeTextChanged()..............................................80

SEND_ACTION......................................................201 bindService()..................................................275, 276

SENDTO_ACTION................................................201 broadcastIntent()....................................210, 261, 262

SMALLEST..............................................................115 broadcastIntentSerialized()..................................210

START_TAG.....................................................161, 162 buildForecasts().....................................................190

SUNDAY..................................................................84 buildQuery()..........................................................178

SYNC_ACTION.....................................................202 bulkInsert()............................................................238

TAB_CATEGORY...................................................202 call()................................................................326-328

TAG_ACTION........................................................223 cancel()...........................................................280, 281

TEST_CATEGORY.................................................202 canGoBack()............................................................111

TEXT.......................................................................161 canGoBackOrForward()..........................................111

TITLE.....................................................................234 canGoForward()......................................................111

UPDATE............................................175, 176, 237, 238 centerMapTo().......................................................302

VERTICAL...............................................................42 check()...............................................................37, 38

VIEW_ACTION.......................200, 202, 209, 217, 314 checkAccount().............................................269, 270

WEB_SEARCH_ACTION......................................202 checkAccountImpl().............................................270

WHERE................175-178, 231, 238, 239, 246, 249-251 checkCallingPermission().....................................261

clear().....................................................................138
Method
clearCache().............................................................111

374

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Keyword Index

clearCheck()............................................................37 getAttributeCount()..............................................162

clearHistory()..........................................................111 getAttributeName()...............................................162

close()................................................147, 174, 179, 180 getBearing()...........................................................290

commit().................................................................138 getBestProvider()..................................................290

commitUpdates()...........................................180, 237 getBoolean()...........................................................138

count()....................................................................179 getCheckedRadioButtonId()...................................37

create()....................................................................119 getCollectionType()...............................................251

createDatabase()..............................................173, 181 getColumnIndex().................................................179

delete().....................................175, 176, 239, 250, 252 getColumnNames()...............................................179

deleteDatabase()....................................................174 getContentProvider()............................................238

deleteInternal().....................................................252 getContentResolver().....................................237, 254

deleteRow()............................................................180 getCurrentLocation()............................................290

dial()...............................................................326-328 getFloat()...............................................................236

draw()............................................................308, 309 getIMAPMessageIds()............................................195

drawCircle()...........................................................309 getInputStream()...................................................239

drawText().............................................................309 getInt()............................................................179, 236

edit().......................................................................138 getIntent().............................................................336

enable()..........................................................268, 277 getLastKnownPosition().......................................290

enablePoll()...........................................................270 getLatitude()..........................................................189

endCall()................................................................326 getLocation().........................................................189

equery()..................................................................179 getLongitude().......................................................189

execSQL()........................................................174, 175 getMapCenter().....................................................303

findViewById()........................27, 28, 40, 94, 143, 301 getMapController()................................................301

finish()............................................................140, 149 getMessageIds().....................................................195

first()...............................................................179, 235 getName().............................................................289

generatePage()........................................................191 getOutputStream()...............................................239

get().........................................................................175 getPackageManager()............................................225

getAltitude()..........................................................290 getParent()...............................................................39

getAsInteger().........................................................175 getParentOfType()..................................................40

getAsString()..........................................................175 getPointXY()..........................................................309

375

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Keyword Index

getPollState().........................................................270 isChecked()........................................................34, 37

getPOP3MessageIds()............................................195 isCollectionUri()............................................248, 250

getPreferences()..............................................137, 138 isEnabled().......................................................39, 276

getProgress()...........................................................90 isFirst()...................................................................236

getProviders()........................................................289 isFocused()..............................................................39

getRequiredColumns().........................................249 isLast()...................................................................236

getResources()........................................................143 isNull()...................................................................236

getRootView().........................................................40 isOffhook()............................................................326

getSettings()............................................................114 isSatellite().............................................................304

getSharedPreferences()...................................137, 138 isStreetView()........................................................304

getSingleType()......................................................251 isTraffic()...............................................................304

getSpeed().............................................................290 isUIThread()...........................................................128

getString().........................................153, 156, 179, 236 last().......................................................................235

getStringArray().....................................................165 loadData().......................................................109, 110

getTitle()................................................................237 loadTime()..............................................................113

getType().........................................................251, 252 loadUrl()...........................................................108-110

getView()....................................................67, 77, 235 makeMeAnAdpater()............................................339

getXml()..................................................................161 makeText()..............................................................118

goBack()...................................................................111 managedQuery().............................................231-234

goBackOrForward()................................................111 move()....................................................................236

goForward().............................................................111 moveTo()................................................................236

handleMessage().............................................124, 126 newCursor()...........................................................180

hasAltitude().........................................................290 newTabSpec()....................................................93, 94

hasBearing()..........................................................290 next()........................................................161, 179, 236

hasSpeed().............................................................290 notify()...........................................................280, 281

incrementProgressBy()...........................................90 notifyChange()...............................................254, 255

insert()......................175, 238, 245, 248, 249, 252, 253 obtainMessage().....................................................124

insertInternal()......................................................252 onActivityResult()..........................................210, 216

isAfterLast()....................................................179, 236 onBind()........................................................266, 269

isBeforeFirst()........................................................236 onCheckedChanged()................................35, 48, 142

376

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Keyword Index

onClick().............................................................20, 21 openFileInput()..............................................147, 149

onCompleteThaw().........................................133, 134 openFileOutput()...........................................147, 149

onContextItemSelected()...............................101, 104 openRawResource()...............................................143

onCreate() 20, 21, 26, 27, 38, 48, 98, 103, 108, 132-134, pause()............................................................315, 324
140, 146, 156, 190, 234, 244, 252, 264-266, 303, 310,
329, 335, 336, 339 play()......................................................................324

onCreateOptionsMenu()...................98, 100, 101, 104 populateDefaultValues()......................................249

onCreatePanelMenu()...........................................100 populateMenu()..............................................103, 104

onDestroy().....................................134, 140, 264, 266 position()...............................................................236

onFreeze()........................................................133, 134 post()...............................................................127, 128

onItemSelected()...................................................365 postDelayed().........................................................127

onKeyUp().............................................................366 prepare().................................................................315

onListItemClick()...................................................69 prepareAsync()........................................315, 319, 320

onNewIntent()...............................................336, 339 prev()......................................................................236

onOptionsItemSelected().........................99-101, 104 putInt()..................................................................237

onPageStarted()......................................................112 putString().............................................................237

onPause()..........................133, 134, 140, 149, 206, 264 query().......................176-178, 180, 246, 247, 252, 253

onPopulateContextMenu()....................................101 queryIntentActivityOptions()..............................225

onReceivedHttpAuthRequest().............................112 queryInternal()......................................................252

onReceiveIntent().........................................205, 206 rawQuery().....................................................176, 180

onRestart().............................................................134 registerContentObserver()...................................254

onResume() 133, 134, 140, 149, 189, 206, 264, 291, 305 registerIntent()......................................................206

onSearchRequested()............................................335 releaseConnection()..............................................188

onServiceConnected()...................................275, 276 reload()....................................................................111

onServiceDisconnected()..............................275, 276 remove().................................................................138

onStart()....................................126, 127, 133, 264, 277 removeProximityAlert()........................................293

onStop()....................................................133, 134, 140 removeUpdates()...................................................292

onTap().............................................................310, 311 requery().........................................................180, 237

onTextChanged()....................................................80 requestFocus().........................................................39

onTooManyRedirects()...........................................112 requestUpdates()...........................................292, 305

openDatabase()......................................................173 runOnUIThread()..................................................128

377

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Keyword Index

sendMessage()........................................................124 setIndicator()....................................................93, 94

sendMessageAtFrontOfQueue()...........................124 setItemCheckable()................................................99

sendMessageAtTime()...........................................124 setJavaScriptCanOpenWindowsAutomatically(). 115

sendMessageDelayed()..........................................124 setJavaScriptEnabled()...........................................115

setAccuracy()........................................................290 setLayoutView()......................................................26

setAdapter()...........................................68, 70, 75, 78 setListAdapter()......................................................69

setAlphabeticShortcut().........................................99 setMessage()...........................................................119

setAltitudeRequired()...........................................290 setNegativeButton()...............................................119

setCellRenderer()....................................................66 setNeutralButton().................................................119

setChecked().......................................34, 38, 142, 276 setNumericShortcut()............................................99

setColumnCollapsed()............................................59 setOnClickListener().......................................20, 149

setColumnShrinkable()..........................................59 setOnCompletionListener()..................................319

setColumnStretchable().........................................59 setOnItemSelectedListener().....................68, 70, 75

setContent()......................................................93, 94 setOnPopulateContextMenuListener()................101

setContentView()..............................................20, 40 setOnPreparedListener().......................................319

setCostAllowed()...................................................290 setOrientation()......................................................42

setCurrentTab()......................................................94 setPadding()............................................................44

setDataSource()......................................................315 setPositiveButton().................................................119

setDefaultFontSize()...............................................115 setProgress()...........................................................90

setDefaultKeyMode()............................................335 setProjectionMap()................................................178

setDropDownViewResource()................................70 setQwertyMode()...................................................99

setDuration()..........................................................118 setResult()..............................................................210

setEnabled()............................................................39 setText()....................................................................21

setFantasyFontFamily()..........................................114 setTextSize()............................................................115

setFollowMyLocation().........................................305 setTitle().................................................................119

setGravity()..............................................................44 setTypeface()...........................................................24

setGroupCheckable().......................................98, 99 setup().............................................................94, 320

setHeader()............................................................100 setupTimer()..........................................265, 266, 270

setIcon()..................................................................119 setUseDesktopUserAgent()....................................115

setImageURI()..........................................................31 setView().................................................................118

378

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Keyword Index

setWebViewClient()...............................................112 updateTime()..........................................................20

shouldOverrideUrlLoading()...........................112, 113 updateView().........................................................307

show().................................................118, 119, 121, 324 upgradeDatabases()..............................................252

showList()..............................................................363 zoomTo()................................................................301

showNotification()................................................282
Property
sRadioOn()............................................................326
android:authorities........................................253, 254
start()......................................................................315
android:autoText......................................................31
startActivity().........................................209, 210, 220
android:background...............................................39
startService().........................................................277
android:capitalize....................................................31
startSubActivity()...........................................210, 216
android:collapseColumns.......................................59
stop()...............................................................315, 320
android:columnWidth............................................74
stopPlayback().......................................................324
android:completionThreshold...............................78
stopService()..........................................................277
android:digits...........................................................31
supportUpdates()...................................................179
android:drawSelectorOnTop.............................71, 82
switch()..................................................................100
android:horizontalSpacing.....................................74
toggle()...............................................................34, 37
android:id.....................................25, 26, 37, 51, 91-93
toggleEdgeZooming()...........................................302
android:indeterminate...........................................90
toggleRadioOnOff()..............................................326
android:indeterminateBehavior............................90
toggleSatellite().....................................................304
android:inputMethod.............................................32
toggleStreetView()................................................304
android:label............................................................13
toggleTraffic()........................................................304
android:layout_above..............................................52
toString().........................................................66, 289
android:layout_alignBaseline.................................52
unbindService().....................................................276
android:layout_alignBottom..................................52
unregisterContentObserver()...............................255
android:layout_alignLeft........................................52
unregisterIntent().................................................206
android:layout_alignParentBottom........................51
update().............................175, 176, 237, 238, 249-253
android:layout_alignParentLeft..............................51
updateInt().............................................................180
android:layout_alignParentRight...........................51
updateInternal()....................................................252
android:layout_alignParentTop.........................51, 55
updateLabel()..........................................................86
android:layout_alignRight......................................52
updateString().......................................................180

379

SAMPLE EDITION - full edition at http://commonsware.com/Android/


Keyword Index

android:layout_alignTop...................................52, 53 android:padding................................................44, 45

android:layout_below.............................................52 android:paddingBottom.........................................45

android:layout_centerHorizontal...........................51 android:paddingLeft...............................................45

android:layout_centerInParent...............................51 android:paddingRight.............................................45

android:layout_centerVertical................................51 android:paddingTop.........................................45, 92

android:layout_column...........................................57 android:password.....................................................31

android:layout_gravity............................................44 android:permission........................................260, 271

android:layout_height...........................25, 43, 53, 92 android:phoneNumber...........................................32

android:layout_span...............................................57 android:progress.....................................................90

android:layout_toLeft.............................................52 android:shrinkColumns..........................................58

android:layout_toRight...........................................52 android:singleLine.............................................31, 32

android:layout_weight............................................43 android:spacing.......................................................82

android:layout_width............................25, 43, 47, 53 android:spinnerSelector.........................................82

android:manifest......................................................12 android:src...............................................................31

android:max.....................................................90, 125 android:stretchColumns.........................................58

android:name.............................13, 253, 258, 270, 341 android:stretchMode..............................................74

android:nextFocusDown........................................39 android:text.......................................................25, 29

android:nextFocusLeft............................................39 android:textColor..............................................30, 34

android:nextFocusRight.........................................39 android:textStyle................................................29, 31

android:nextFocusUp.............................................39 android:typeface.....................................................29

android:numColumns.............................................74 android:value.........................................................341

android:numeric......................................................31 android:verticalSpacing..........................................74

android:orientation................................................42 android:visibility.....................................................39

380

SAMPLE EDITION - full edition at http://commonsware.com/Android/

You might also like