Professional Android ™ Application Development www.wrox.com $44.99 USA $48.99 CANADA Wrox Professional guides are planned and written by working programmers to meet the real-world needs of programmers, developers, and IT professionals. Focused and relevant, they address the issues technology professionals face every day. They provide examples, practical solutions, and expert education in new technologies, all designed to help programmers do a better job. Recommended Computer Book Categories Programming Languages Java ISBN: 978-0-470-34471-2 Offering an open development environment, Android represents an exciting new opportunity to write innovative applications for mobile devices. This book provides you with a hands-on guide to building these applications using the Android software development kit. It takes you through a series of sample projects, each introducing new features and techniques to get the most out of Android. You’ll learn all about the basic functionality as well as discover how to utilize the advanced features with the help of concise and useful examples. Beginning with an introduction to the Android software stack, the author examines the philosophy behind creating robust, consistent, and appealing applications for mobile phones. You’ll get the grounding and knowledge that is needed to write customized mobile applications using the current Android 1.0 SDK. Plus, you’ll also gain the flexibility to quickly adapt to future enhancements in order to build the most cutting-edge solutions. What you will learn from this book ● Best practices for Android mobile development ● An introduction to Activities, Intents, the manifest, and resources ● How to create user interfaces with layouts and custom views ● Techniques to store and share your application data ● Instructions for creating map-based applications, using location-based services including GPS, and geocoding locations ● How to create and use background Services and Notifications ● Working with the accelerometers, compass, and camera hardware ● All about phone and networking hardware such as telephony APIs, SMS, and network management ● Advanced development topics, including security, IPC, and some advanced graphics and user interface techniques Who this book is for This book is for anyone interested in creating applications for the Android mobile phone platform. It includes information that will be valuable whether you’re an experienced mobile developer or just starting out writing mobile applications. Enhance Your Knowledge Advance Your Career Android ™ Application Development Meier Professional subtitle spine=.864" Updates, source code, and Wrox technical support at www.wrox.com Reto Meier Wrox Programmer to Programmer TM Wrox Programmer to Programmer TM Professional Android ™ Application Development Professional Android ™ Application Development Enhance Your Knowledge Advance Your Career Professional Android Application Development 978-0-470-34471-2 A hands-on guide to building mobile applications, this book features concise and compelling examples that show you how to quickly construct real-world mobile applications for Android phones. Fully up-to-date for version 1.0 of the Android SDK, it covers all the essential features, and explores the advanced capabilities of Android. Professional Java JDK 6 Edition 978-0-471-77710-6 Building upon Ivor Horton’s Beginning Java 2, this resource shows you how to use the core features of the latest JDK as well as powerful open source tools such as Ant, JUnit, and Hibernate. It will arm you with a well-rounded understanding of the professional Java development landscape. Expert One-on-One TM J2EETM Development without EJBTM 978-0-7645-5831-3 This hands-on guide shows you alternatives to EJB that can be used to create higher quality applications faster and at lower cost, and demonstrates how to leverage practical techniques and tools, including the popular open source Spring Framework and Hibernate. spine=.864" Get more out of WROX.com Programmer to Programmer ™ Interact Take an active role online by participating in our P2P forums Wrox Online Library Hundreds of our books are available online through Books24x7.com Wrox Blox Download short informational pieces and code to keep you up to date and out of trouble! Chapters on Demand Purchase individual book chapters in pdf format Join the Community Sign up for our free monthly newsletter at newsletter.wrox.com Browse Ready for more Wrox? We have books and e-books available on .NET, SQL Server, Java, XML, Visual Basic, C#/ C++, and much more! Contact Us. We always like to get feedback from our readers. Have a book idea? Need community support? Let us know by e-mailing wrox-partnerwithus@wrox.com Professional Android ™ Application Development Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Chapter 1: Hello, Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 2: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Chapter 3: Creating Applications and Activities . . . . . . . . . . . . . . . . . . . . . . 45 Chapter 4: Creating User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Chapter 5: Intents, Broadcast Receivers, Adapters, and the Internet . . . . . . 113 Chapter 6: Data Storage, Retrieval, and Sharing . . . . . . . . . . . . . . . . . . . . . 159 Chapter 7: Maps, Geocoding, and Location-Based Services . . . . . . . . . . . . . 207 Chapter 8: Working in the Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Chapter 9: Peer-to-Peer Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Chapter 10: Accessing Android Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . 315 Chapter 11: Advanced Android Development . . . . . . . . . . . . . . . . . . . . . . . . 353 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399 44712ffirs.indd i44712ffirs.indd i 10/20/08 4:08:56 PM10/20/08 4:08:56 PM 44712ffirs.indd ii44712ffirs.indd ii 10/20/08 4:08:56 PM10/20/08 4:08:56 PM Professional Android ™ Application Development Reto Meier 44712ffirs.indd iii44712ffirs.indd iii 10/20/08 4:08:56 PM10/20/08 4:08:56 PM Professional Android ™ Application Development Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2009 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-0-470-34471-2 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 Library of Congress Cataloging-in-Publication Data is available from the publisher. No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permis- sion of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at http://www.wiley.com/go/permissions. Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or war- ranties with respect to the accuracy or completeness of the contents of this work and specifi cally disclaim all warranties, including without limitation warranties of fi tness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read. For general information on our other products and services please contact our Customer Care Department within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affi liates, in the United States and other countries, and may not be used without written permission. Android is a trademark of Google, Inc. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. 44712ffirs.indd iv44712ffirs.indd iv 10/20/08 4:08:56 PM10/20/08 4:08:56 PM To Kris 44712ffirs.indd v44712ffirs.indd v 10/20/08 4:08:56 PM10/20/08 4:08:56 PM About the Author Originally from Perth, Western Australia, Reto Meier now lives in London. Reto is an experienced software developer with more than 10 years of experience in GUI application architecture, design, and development. He’s worked in various industries, including offshore oil and gas, before moving to London and into fi nance. Always interested in emerging technologies, Reto has been involved in Android since the initial release in 2007. In his spare time, he tinkers with a wide range of development platforms including WPF and Google’s plethora of developer tools. You can check out Reto’s web site, The Radioactive Yak, at http://blog.radioactiveyak.com. About the Tech Editor Dan Ulery is a software engineer with experience in .NET, Java, and PHP development, as well as in deployment engineering. He graduated from the University of Idaho with a bachelor of science degree in computer science and a minor in mathematics. 44712ffirs.indd vi44712ffirs.indd vi 10/20/08 4:08:56 PM10/20/08 4:08:56 PM Credits Executive Editor Chris Webb Development Editor William Bridges Technical Editor Daniel Ulery Senior Production Editor Debra Banninger Copy Editor Cate Caffrey Editorial Manager Mary Beth Wakefi eld Production Manager Tim Tate Vice President and Executive Group Publisher Richard Swadley Vice President and Executive Publisher Joseph B. Wikert Project Coordinator, Cover Lynsey Stanford Compositor James D. Kramer, Happenstance Type-O-Rama Proofreader Nancy Carrasco Indexer Jack Lewis 44712ffirs.indd vii44712ffirs.indd vii 10/20/08 4:08:56 PM10/20/08 4:08:56 PM Acknowledgments A big thank you goes to the Android team, particularly those of you who’ve contributed to the Android developer Google Groups, for creating and supporting an exciting new playground. I also thank Philipp Lenssen for providing an inspiration, and occasional venue, for my blogging efforts; Chris Webb for reading a blog and seeing an author; and Bill Bridges, Dan Ulery, and the Wrox team for helping me along the way. Thanks also to Paul, Stu, and Mike: Your friendship and inspiration helped me get to where I am. Most importantly, I’d like to thank Kristy. For everything. 44712ffirs.indd viii44712ffirs.indd viii 10/20/08 4:08:56 PM10/20/08 4:08:56 PM [...]... Introducing Some Android- Supplied Adapters Using Adapters for Data Binding Using Internet Resources Connecting to an Internet Resource Leveraging Internet Resources Introducing Dialogs 79 79 80 81 85 88 98 99 99 10 1 10 4 10 4 10 5 10 7 11 2 11 3 11 4 11 4 12 1 13 0 13 2 13 6 13 6 13 6 14 1 14 2 14 3 14 3 Introducing the Dialog Class Using Activities as Dialogs 14 4 14 7 Creating an Earthquake Viewer Summary 14 8 15 7 Chapter... MyLocationOverlay Introducing ItemizedOverlays and OverlayItems Pinning Views to the Map and Map Positions 17 4 17 4 17 5 17 5 17 6 17 6 17 7 18 9 18 9 19 2 19 4 19 7 205 207 208 208 208 209 212 212 212 213 214 216 217 219 220 2 21 2 21 222 224 224 224 226 227 228 2 31 239 239 240 xii 44 712 ftoc.indd xii 10 / 21/ 08 12 :07 :15 AM Contents Mapping Earthquakes Example Summary Chapter 8: Working in the Background Introducing Services... Libraries 12 12 14 14 15 16 Summary 44 712 ftoc.indd ix 17 10 / 21/ 08 12 :07 :15 AM Contents Chapter 2: Getting Started Developing for Android What You Need to Begin Creating Your First Android Activity Types of Android Applications Developing for Mobile Devices Hardware-Imposed Design Considerations Considering the Users’ Environment Developing for Android To-Do List Example Android Development Tools The Android. .. 280 2 81 282 283 286 289 2 91 2 91 292 294 297 306 314 xiii 44 712 ftoc.indd xiii 10 / 21/ 08 12 :07 :15 AM Contents Chapter 10 : Accessing Android Hardware Using the Media APIs Playing Media Resources Recording Multimedia Using the Camera Controlling Camera Settings Using the Camera Preview Taking a Picture 315 316 316 317 319 319 320 320 Introducing the Sensor Manager Using the Accelerometer and Compass 3 21 323... Does Android Run On? Why Develop for Android? What Will Drive Android Adoption? What Does It Have That Others Don’t? Changing the Mobile Development Landscape xvii 1 2 2 3 3 4 4 5 6 6 6 7 7 7 8 8 8 9 9 10 10 11 Introducing the Development Framework 11 What Comes in the Box Understanding the Android Software Stack The Dalvik Virtual Machine Android Application Architecture Android Libraries Advanced Android. .. and Sharing Android Techniques for Saving Data Saving Simple Application Data Creating and Saving Preferences Retrieving Shared Preferences Saving the Activity State Creating a Preferences Page for the Earthquake Viewer 15 9 16 0 16 0 16 1 16 1 16 2 16 5 xi 44 712 ftoc.indd xi 10 / 21/ 08 12 :07 :15 AM Contents Saving and Loading Files Including Static Files as Resources File Management Tools Databases in Android Introducing... click the FAQ link on any P2P page xxi 44 712 flast.indd xxi 10 / 21/ 08 12 :11 :04 AM 44 712 flast.indd xxii 10 / 21/ 08 12 :11 :04 AM Hello, Android Whether you’re an experienced mobile engineer, a desktop or web developer, or a complete programming novice, Android represents an exciting new opportunity to write innovative applications for mobile devices Despite the name, Android will not help you create an unstoppable... Services Building Rich User Interfaces Working with Animations Using Themes to Skin Your Applications Advanced Canvas Drawing Introducing SurfaceView Creating Interactive Controls Summary Index 356 356 3 61 3 61 3 61 372 373 390 393 398 399 xv 44 712 ftoc.indd xv 10 / 21/ 08 12 :07 :15 AM 44 712 flast.indd xvi 10 / 21/ 08 12 :11 :04 AM Introduction Now is an exciting time for mobile developers Mobile phones have never... introduction to the Android libraries, and a look at the Dalvik virtual machine 11 44 712 c 01. indd 11 10 /20/08 4 :12 :24 PM Chapter 1: Hello, Android Each Android application runs in a separate process within its own Dalvik instance, relinquishing all responsibility for memory and process management to the Android run time, which stops and kills processes as necessary to manage resources Dalvik and the Android run... dictates 44 712 flast.indd xvii 10 / 21/ 08 12 :11 :04 AM Introduction What This Book Covers Chapter 1 introduces Android, including what it is and how it fits into existing mobile development What Android offers as a development platform and why it’s an exciting opportunity for creating mobile phone applications are then examined in greater detail Chapter 2 covers some best practices for mobile development . 393 Summary 398 Index 399 44 712 ftoc.indd xv44 712 ftoc.indd xv 10 / 21/ 08 12 :07 :15 AM10/ 21/ 08 12 :07 :15 AM 44 712 flast.indd xvi44 712 flast.indd xvi 10 / 21/ 08 12 :11 :04 AM10/ 21/ 08 12 :11 :04 AM Introduction Now. xiii44 712 ftoc.indd xiii 10 / 21/ 08 12 :07 :15 AM10/ 21/ 08 12 :07 :15 AM xiv Contents Accessing Android Hardware 31Chapter 10 : 5 Using the Media APIs 316 Playing Media Resources 316 Recording Multimedia 317 Using. Viewer 16 5 44 712 ftoc.indd xi44 712 ftoc.indd xi 10 / 21/ 08 12 :07 :15 AM10/ 21/ 08 12 :07 :15 AM xii Contents Saving and Loading Files 17 4 Including Static Files as Resources 17 4 File Management Tools 17 5 Databases