spine=.864" Wrox Programmer to ProgrammerTM Wrox Programmer to ProgrammerTM Professional Meier Android Application Development ™ Offering an open development environment, Android represents an exciting Professional new opportunity to write innovative applications for mobile devices. This book provides you with a hands-on guide to building these applications using the A 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 n Android. You’ll learn all about the basic functionality as well as discover how to d utilize the advanced features with the help of concise and useful examples. r Beginning with an introduction to the Android software stack, the author o examines the philosophy behind creating robust, consistent, and appealing i applications for mobile phones. You’ll get the grounding and knowledge that is d 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. A What you will learn from this book p ● Best practices for Android mobile development p ● An introduction to Activities, Intents, the manifest, and resources ● How to create user interfaces with layouts and custom views li Professional ● Techniques to store and share your application data c ● Instructions for creating map-based applications, using location-based a services including GPS, and geocoding locations t ● How to create and use background Services and Notifications i ● Working with the accelerometers, compass, and camera hardware Enhance Your Knowledge o Android™ ● All about phone and networking hardware such as telephony APIs, SMS, and Advance Your Career n network management ● Advanced development topics, including security, IPC, and some advanced D graphics and user interface techniques e Application Who this book is for v This book is for anyone interested in creating applications for the Android mobile phone platform. It includes information that will be e valuable whether you’re an experienced mobile developer or just starting out writing mobile applications. l o Wrox Professional guides are planned and written by working programmers to meet the real-world needs of programmers, developers, p Development and IT professionals. Focused and relevant, they address the issues technology professionals face every day. They provide examples, subtitle practical solutions, and expert education in new technologies, all designed to help programmers do a better job. m e www.wrox.com n t Recommended Programming Languages ISBN: 978-0-470-34471-2 Computer Book Reto Meier Categories Java $44.99 USA $48.99 CANADA Updates, source code, and Wrox technical support at www.wrox.com spine=.864" Professional ™ Programmer to Programmer Android Application Development ™ Get more out of 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 WROX.com 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 Interact Chapters on Demand will arm you with a well-rounded understanding of the professional Java development landscape. Take an active role online by participating in Purchase individual book chapters in pdf our P2P forums format Expert One-on-OneTM J2EETM Development without EJBTM 978-0-7645-5831-3 Wrox Online Library Join the Community 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 Hundreds of our books are available online Sign up for our free monthly newsletter at tools, including the popular open source Spring Framework and Enhance Your Knowledge through Books24x7.com newsletter.wrox.com Hibernate. Advance Your Career Wrox Blox Browse Download short informational pieces and Ready for more Wrox? We have books and code to keep you up to date and out of e-books available on .NET, SQL Server, Java, trouble! 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 [email protected] 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 4444771122ffffiirrss..iinndddd ii 1100//2200//0088 44::0088::5566 PPMM 4444771122ffffiirrss..iinndddd iiii 1100//2200//0088 44::0088::5566 PPMM Professional Android™ Application Development Reto Meier 4444771122ffffiirrss..iinndddd iiiiii 1100//2200//0088 44::0088::5566 PPMM 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. 4444771122ffffiirrss..iinndddd iivv 1100//2200//0088 44::0088::5566 PPMM To Kris 4444771122ffffiirrss..iinndddd vv 1100//2200//0088 44::0088::5566 PPMM 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. 4444771122ffffiirrss..iinndddd vvii 1100//2200//0088 44::0088::5566 PPMM Credits Executive Editor Vice President and Executive Group Publisher Chris Webb Richard Swadley Development Editor Vice President and Executive Publisher William Bridges Joseph B. Wikert Technical Editor Project Coordinator, Cover Daniel Ulery Lynsey Stanford Senior Production Editor Compositor Debra Banninger James D. Kramer, Happenstance Type-O-Rama Copy Editor Proofreader Cate Caffrey Nancy Carrasco Editorial Manager Indexer Mary Beth Wakefi eld Jack Lewis Production Manager Tim Tate 4444771122ffffiirrss..iinndddd vviiii 1100//2200//0088 44::0088::5566 PPMM 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. 4444771122ffffiirrss..iinndddd vviiiiii 1100//2200//0088 44::0088::5566 PPMM