i
Pro Android 3
■ ■ ■
Satya Komatineni
Dave MacLean
Sayed Y. Hashimi
Pro Android 3
Copyright © 2011 by Satya Komatineni, Dave MacLean, and Sayed Y. Hashimi
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any
means, electronic or mechanical, including photocopying, recording, or by any information
storage or retrieval system, without the prior written permission of the copyright owner and the
publisher.
ISBN-13 (pbk): 978-1-4302-3222-3
ISBN-13 (electronic): 978-1-4302-3223-0
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occurrence of a trademarked name, logo, or image we use the names, logos,
and images only in an editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark. NFC Forum and the NFC Forum logo are trademarks
of the Near Field Communication Forum.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
President and Publisher: Paul Manning
Lead Editor: Matthew Moodie
Technical Reviewer: Dylan Phillips
Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan
Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank
Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade,
Tom Welsh
Coordinating Editor: Corbin Collins
Copy Editors: Heather Lang, Tracy Brown, Mary Behr
Compositor: MacPS, LLC
Indexer: BIM Indexing & Proofreading Services
Artist: April Milne
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring
Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
orders-ny@springer-sbm.com, or visit www.springeronline.com.
For information on translations, please e-mail rights@apress.com, or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or
promotional use. eBook versions and licenses are also available for most titles. For more
information, reference our Special Bulk Sales–eBook Licensing web page at
www.apress.com/info/bulksales.
The information in this book is distributed on an “as is” basis, without warranty. Although every
precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall
have any liability to any person or entity with respect to any loss or damage caused or alleged to
be caused directly or indirectly by the information contained in this work.
The source code for this book is available to readers at www.apress.com.
To my brother Hari, to whom life yielded few favors.
—Satya Komatineni
To my wife, Rosie, and my son, Mike, for their support; I couldn't have done this without you. And
to Max, for spending so much time at my feet keeping me company.
—Dave MacLean
To my son, Sayed-Adieb.
—Sayed Y. Hashimi
iv
Contents at a Glance
Contents ....................................................................................................................................................... vi
Foreword ................................................................................................................................................... xviii
About the Authors ....................................................................................................................................... xix
About the Technical Reviewer ..................................................................................................................... xx
Acknowledgments ...................................................................................................................................... xxi
Preface ....................................................................................................................................................... xxii
■
Chapter 1: Introducing the Android Computing Platform .......................................................................... 1
■
Chapter 2: Setting Up Your Development Environment ........................................................................... 21
■
Chapter 3: Understanding Android Resources ......................................................................................... 63
■
Chapter 4: Understanding Content Providers .......................................................................................... 89
■
Chapter 5: Understanding Intents .......................................................................................................... 125
■
Chapter 6: Building User Interfaces and Using Controls ................................ ........................................ 145
■
Chapter 7: Working with Menus ............................................................................................................ 217
■
Chapter 8: Working with Dialogs ........................................................................................................... 243
■
Chapter 9: Working with Preferences and Saving State ........................................................................ 265
■
Chapter 10: Exploring Security and Permissions .................................................................................. 287
■
Chapter 11: Building and Consuming Services ...................................................................................... 307
■
Chapter 12: Exploring Packages ............................................................................................................ 377
■
Chapter 13: Exploring Handlers ............................................................................................................. 399
■
Chapter 14: Broadcast Receivers and Long-Running Services .............................................................. 425
■
Chapter 15: Exploring the Alarm Manager ............................................................................................ 465
■
Chapter 16: Exploring 2D Animation ...................................................................................................... 491
■
Chapter 17: Exploring Maps and Location-based Services ................................................................... 519
■
Chapter 18: Using the Telephony APIs ................................................................................................... 559
■
Chapter 19: Understanding the Media Frameworks .............................................................................. 575
■
Chapter 20: Programming 3D Graphics with OpenGL ............................................................................ 623
■
Chapter 21: Exploring Live Folders ........................................................................................................ 693
■
Chapter 22: Home Screen Widgets ........................................................................................................ 711
■
Chapter 23: Android Search ................................................................ ................................................... 745
■
Chapter 24: Exploring Text to Speech .................................................................................................... 825
■
Chapter 25: Touch Screens .................................................................................................................... 845
■
Chapter 26: Using Sensors ..................................................................................................................... 891
■
Chapter 27: Exploring the Contacts API ................................................................................................. 937
■
Chapter 28: Deploying Your Application: Android Market and Beyond ................................................. 993
■
Chapter 29: Fragments for Tablets and More ...................................................................................... 1015
■
Chapter 30: Exploring ActionBar ......................................................................................................... 1069
■
Chapter 31: Additional Topics in 3.0 .................................................................................................... 1097
Index ....................................................................................................................................................... 1141
v
Contents
Contents at a Glance .......................................................................................... iv
Foreword ........................................................................................................ xviii
About the Authors ............................................................................................. xix
About the Technical Reviewer ........................................................................... xx
Acknowledgments ............................................................................................ xxi
Preface ............................................................................................................ xxii
■
Chapter 1: Introducing the Android Computing Platform ................................ 1
A New Platform for a New Personal Computer ....................................................................................................... 1
Early History of Android .......................................................................................................................................... 3
Delving Into the Dalvik VM ...................................................................................................................................... 6
Understanding the Android Software Stack ............................................................................................................ 6
Developing an End-User Application with the Android SDK .................................................................................... 8
Android Emulator ............................................................................................................................................... 8
The Android UI ................................................................................................................................................... 9
The Android Foundational Components ........................................................................................................... 10
Advanced UI Concepts ..................................................................................................................................... 11
Android Service Components ........................................................................................................................... 13
Android Media and Telephony Components .................................................................................................... 13
Android Java Packages .................................................................................................................................... 14
Taking Advantage of Android Source Code ........................................................................................................... 18
The Sample Projects in this Book ......................................................................................................................... 19
Summary .............................................................................................................................................................. 20
■
Chapter 2: Setting Up Your Development Environment ................................. 21
Setting Up Your Environment ................................................................................................................................ 22
Downloading JDK 6 .......................................................................................................................................... 22
Downloading Eclipse 3.6 .................................................................................................................................. 23
Downloading the Android SDK ......................................................................................................................... 23
The Tools Window ............................................................................................................................................ 26
Installing Android Development Tools (ADT) .................................................................................................... 26