Android™ Wireless Application Development This page intentionally left blank Android Wireless Application Development Shane Conder Lauren Darcey Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Cape Town • Sydney • Tokyo • Singapore • Mexico City Many of the designations used by manufacturers and sellers to distinguish their products Editor-in-Chief are claimed as trademarks. Where those designations appear in this book,and the publish- Mark Taub er was aware of a trademark claim,the designations have been printed with initial capital Acquisitions Editor letters or in all capitals. Trina MacDonald The authors and publisher have taken care in the preparation of this book,but make no Development expressed or implied warranty of any kind and assume no responsibility for errors or omis- Editor sions. No liability is assumed for incidental or consequential damages in connection with or Songlin Qiu arising out of the use of the information or programs contained herein. Managing Editor The publisher offers excellent discounts on this book when ordered in quantity for bulk pur- Patrick Kanouse chases or special sales,which may include electronic versions and/or custom covers and Project Editor content particular to your business,training goals,marketing focus,and branding interests. Bethany Wall For more information,please contact: Copy Editor U.S. Corporate and Government Sales Apostrophe Editing (800) 382-3419 Services [email protected] Indexer For sales outside the United States please contact: Tim Wright International Sales Proofreader [email protected] Jovana San Nicolas- Shirley Visit us on the Web: informit.com/aw Technical Library of Congress Cataloging-in-Publication Data: Reviewers Dan Galpin Conder,Shane,1975- Tony Hillerson Android wireless application development / Shane Conder,Lauren Darcey. Ronan Schwarz p. cm. Includes bibliographical references and index. Publishing ISBN 978-0-321-62709-4 (pbk. : alk. paper) 1. Mobile computing. 2. Coordinator Android (Electronic resource) 3. Application software--Development. 4. Olivia Basegio Wireless communication systems. I. Darcey,Lauren,1977- II. Title. Multimedia QA76.59.C65 2009 Developer 621.3845'6--dc22 Dan Scherf 2009027111 Designer Copyright © 2010 Shane Conder and Lauren Darcey Gary Adair All rights reserved. Printed in the United States of America. This publication is protected by Compositor copyright,and permission must be obtained from the publisher prior to any prohibited repro- Bronkella duction,storage in a retrieval system,or transmission in any form or by any means,elec- Publishing,LLC tronic,mechanical,photocopying,recording,or likewise. For information regarding permis- sions,write to: Pearson Education,Inc Rights and Contracts Department 501 Boylston Street,Suite 900 Boston,MA 02116 Fax (617) 671-3447 Android is the trademark of Google,Inc. Pearson Education does not assert any right to the use of the Android trademark and neither Google nor any other third party having any claim in the Android trademark have sponsored or are affiliated with the creation and develop- ment of this book. This material may be distributed only subject to the terms and conditions set forth in the Open Publication License,v1.0 or later (the latest version is presently available at www.opencontent.org/openpub/). ISBN-13: 978-0-321-62709-4 ISBN-10: 0-321-62709-1 Text printed in the United States on recycled paper at RR Donnelley Crawfordsville,Indiana First printing August 2010 ❖ This book is dedicated to Bit,Nibble,Stack,Queue, Heap,and Null. ❖ This page intentionally left blank Table of Contents Introduction 1 Who Should Read This Book 1 Key Questions Answered in this Book 2 How This Book Is Structured 2 Android Development Environment Used in This Book 3 Supplementary Materials Available 4 Where to Find More Information 4 Conventions Used in This Book 5 Contacting the Authors 5 I: An Overview of Android 1 Introducing Android 9 A Brief History of Mobile Software Development 9 Way Back When 9 “The Brick” 11 Wireless Application Protocol (WAP) 12 Proprietary Mobile Platforms 14 The Open Handset Alliance 16 Google Goes Wireless 17 Forming of the Open Handset Alliance 17 Manufacturers: Designing the Android Handsets 18 Content Providers: Developing Android Applications 18 Mobile Operators: Delivering the Android Experience 18 Taking Advantage of All Android Has to Offer 18 Android Platform Differences 19 Android: A Next Generation Platform 19 Free and Open Source 20 Familiar and Inexpensive Development Tools 20 Reasonable Learning Curve for Developers 21 Enabling Development of Powerful Applications 21 Rich,Secure Application Integration 21 No Costly Obstacles to Publication 22 A “Free Market” for Applications 22 A New and Growing Platform 23 The Android Platform 23 Android’s Underlying Architecture 24 Security and Permissions 26 Developing Android Applications 27 Summary 29 References and More Information 29 2 Your Android Development Environment 31 Configuring Your Development Environment 31 Supported Operating Systems 31 Installing the Java Development Kit 32 Installing the Eclipse Development Environment for Java 32 Other Development Environments 33 Installing the Android Software Development Kit 34 Installing and Configuring the Android Plug-In for Eclipse (ADT) 35 Troubleshooting the ADT Installation 37 Upgrading the Android Software Development Kit 37 Problems with the Android Software Development Kit 38 Configuring Your Android Hardware for Debugging 38 Configuring Your Operating System for Device Debugging 38 The Development Environment Used in This Book 39 Exploring the Android Software Development Kit 39 The Android SDK License Agreement 39 The Android SDK Documentation 41 The Android Application Framework 42 The Android Tools 43 The Android Sample Applications 47 Summary 47 References and More Information 47 3 Writing Your First Android Application 49 Testing Your Development Environment 49 Adding the Snake Application to a Project in Your Eclipse Workspace 50 Creating an Android Virtual Device (AVD) for Your Snake Project 52 Creating a Launch Configuration for Your Snake Project 52 Running the Snake Application in the Android Emulator 54 Building Your First Android Application 55 Creating and Configuring a New Android Project 55 Core Files and Directories of the Android Application 56 Creating an AVD for Your Project 57 Creating a Launch Configuration for Your Project 58 Running Your Android Application in the Emulator 58 Debugging Your Android Application in the Emulator 61 Adding Logging Support to Your Android Application 63 Adding Some Media Support to Your Application 64 Adding Some Location-Based Services Support to Your Application 66 Debugging Your Application on the Hardware 68 Summary 69 References and More Information 69 II: Android Application Design Essentials 4 Understanding the Anatomy of an Android Application 73 The Life Cycle of an Android Application 73 Performing Application Tasks with Activities 73 Transitioning Between Activities with Intents 74 The Lifecycle of an Android Activity 77 Defining Your Application Using the Android Manifest File 82