Android : A Programmer's Guide

Transcription

Android A Programmer’s Guide

This page intentionally left blank

Android A Programmer’s GuideJ.F. DiMarzioNew York Chicago San FranciscoLisbon London Madrid Mexico CityMilan New Delhi San JuanSeoul Singapore Sydney Toronto

Copyright 2008 by The McGraw-Hill Companies. All rights reserved. Manufactured in the United States of America. Except as permittedunder the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, orstored in a database or retrieval system, without the prior written permission of the publisher.0-07-159989-4The material in this eBook also appears in the print version of this title: 0-07-159988-6.All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name,we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of thetrademark. Where such designations appear in this book, they have been printed with initial caps.McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate trainingprograms. For more information, please contact George Hoare, Special Sales, at george hoare@mcgraw-hill.com or (212) 904-4069.TERMS OF USEThis is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors reserve all rights in and to the work. Useof this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of thework, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute,disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent. You may use the work for your ownnoncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail tocomply with these terms.THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THEACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANYINFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor itslicensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for anydamages resulting therefrom. McGraw-Hill has no responsibility for the content of any information accessed through the work. Under nocircumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damagesthat result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. Thislimitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise.DOI: 10.1036/0071599886

ProfessionalWant to learn more?We hope you enjoy thisMcGraw-Hill eBook! Ifyou’d like more information about this book,its author, or related books and websites,please click here.

This book is dedicated to Suzannah, Christian, and Sophia

About the AuthorJ.F. DiMarzio is a developer with over 15 years ofexperience in networking and application developmentand is the author of seven books on computing technologies.He has become a leading resource in the fields of ITconsulting and development. He lives in Central Florida.About the Technical EditorGilbert L. Polo is a software developer with over 20years of experience working in the telecommunications,financial, and, most recently, educational industries. Hehas programmed in various languages including C, C ,Java, and C#.Copyright 2008 by The McGraw-Hill Companies. Click here for terms of use.

Contents at a Glance1 What Is Android? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12 Downloading and Installing Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .93 Downloading and Installing the Android SDK.21.355 Application: Hello World! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .536 Using the Command-Line Tools and the Android Emulator . . . . . . . . . . . .834 Exploring the Android SDK7 Using Intents and the Phone Dialer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1178 Lists, Menus, and Other Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1499 Using the Cell Phone’s GPS Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . 20310 Using the Google API with GTalk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23911 Application: Find a Friend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25712 Android SDK Tool Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313vii

This page intentionally left blank

For more information about this title, click hereContentsACKNOWLEDGMENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xiiixv1 What Is Android? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Brief History of Embedded Device Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Open Handset Alliance and Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Introduction to Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12 Downloading and Installing Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Why Eclipse? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Downloading and Installing the JRE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Downloading and Installing Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Downloading and Installing the Android SDK . . . . . . . . . . . . . . . . . . . . . . .Downloading the Android SDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Downloading and Installing the Android Plugin for Eclipse . . . . . . . . . . . . . . . . . . . . . .Configuring the Android Plugin for Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .ix256911121821242430

xAndroid: A Programmer’s Guide4 Exploring the Android SDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .What Is in the Android SDK? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Android Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Android Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Try This: Run the API Demos Sample Application . . . . . . . . . . . . . . . . . . . . . . . . .Android Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Application Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Standard ASP Application Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Android Application Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .355 Application: Hello World! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating Your First Android Project in Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Examining the Android-Created Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .AndroidManifest.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Referenced Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Hello World! Again . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Hello World! Using an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Hello World! Code-Based UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Hello World! XML-Based UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Try This: Use TextView and ImageView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53373839414548494950556162626369727578816 Using the Command-Line Tools and the Android Emulator . . . . . . . . . . . . 83Creating a Shell Activity Using the Windows CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84Running the ActivityCreator.bat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85The Project Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88Creating the Hello World! Activity in the Windows CLI . . . . . . . . . . . . . . . . . . . . . . . . 95Editing the Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95Adding the JAVA HOME Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96Compiling and Installing the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97Hello World! on Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109Configuring the PATH Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109Try This: Create an Image-Based Hello World! in the CLI . . . . . . . . . . . . . . . . . . . 1157 Using Intents and the Phone Dialer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .What Are Intents? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using the Dialer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Placing a Call from Your Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Adding the Intent to Your Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Editing Activity Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117119124128129131

ContentsModifying the AndroidPhoneDialer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Adding a Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Implementing an EditText View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Try This: Modify the AndroidPhoneDialer Project . . . . . . . . . . . . . . . . . . . . . . . . . .1361361411458 Lists, Menus, and Other Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Building the Activities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Intent Code for the .xml File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Intent Code for the .java File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Modifying the AndroidManifest.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using the Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating the Activity for AutoComplete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .CheckBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .EditText . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .RadioGroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Spinner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Try This: Modify More View Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1499 Using the Cell Phone’s GPS Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . .Using the Android Location-Based API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating a kml File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .What Is a track File? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .G

Creating Your First Android Project in Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Examining the Android-Created Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 AndroidManifest.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62