SECOND EDITION
Programming Android
Zigurd Mednieks, Laird Dornin, G. Blake Meike,
and Masumi Nakamura
Beijing
•
Cambridge
•
Farnham
•
Köln
•
Sebastopol
•
Tokyo
Downloa d f r o m W o w ! e B o o k < w w w.woweb o o k . c o m >
Programming Android, Second Edition
by Zigurd Mednieks, Laird Dornin, G. Blake Meike, and Masumi Nakamura
Copyright © 2012 Zigurd Mednieks, Laird Dornin, Blake Meike, and Masumi Nakamura. All rights
reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (http://my.safaribooksonline.com). For more information, contact our
corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com.
Editors: Andy Oram and Rachel Roumeliotis
Production Editor: Melanie Yarbrough
Copyeditor: Audrey Doyle
Proofreader: Teresa Horton
Indexer: Ellen Troutman-Zaig
Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrator: Robert Romano
September 2012: Second Edition.
Revision History for the Second Edition:
2012-09-26 First release
See http://oreilly.com/catalog/errata.csp?isbn=9781449316648 for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. Programming Android, Second Edition, the cover image of a pine grosbeak, and
related trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information con-
tained herein.
ISBN: 978-1-449-31664-8
[LSI]
1348682639
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Part I. Tools and Basics
1. Installing the Android SDK and Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Installing the Android SDK and Prerequisites 3
The Java Development Kit (JDK) 4
The Eclipse Integrated Development Environment (IDE) 5
The Android SDK 7
Adding Build Targets to the SDK 8
The Android Developer Tools (ADT) Plug-in for Eclipse 9
Test Drive: Confirm That Your Installation Works 12
Making an Android Project 12
Making an Android Virtual Device (AVD) 16
Running a Program on an AVD 19
Running a Program on an Android Device 20
Troubleshooting SDK Problems: No Build Targets 21
Components of the SDK 21
The Android Debug Bridge (adb) 21
The Dalvik Debug Monitor Server (DDMS) 21
Components of the ADT Eclipse Plug-in 23
Android Virtual Devices 25
Other SDK Tools 26
Keeping Up-to-Date 27
Keeping the Android SDK Up-to-Date 28
Keeping Eclipse and the ADT Plug-in Up-to-Date 28
Keeping the JDK Up-to-Date 29
Example Code 30
SDK Example Code 30
Example Code from This Book 30
On Reading Code 31
iii