CYAN YELLOW SPOT MATTE MAGENTA BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS® Companion eBook Available A ndroid, Google’s open-source platform for mobile development, has Covers Google’s Android 2 Platform including advanced the momentum to become the leading mobile platform. Pro Android 2 P shows you how to build real-world mobile apps using Google’s Android SDK. topics such as OpenGL, Widgets, Text to Speech, Android is easy to learn yet comprehensive, and is rich in functionality. r o Multi-Touch, and Titanium Mobile The absence of licensing fees for Android OS has borne fruit already with many distinct device manufacturers and a multiplicity of models and carriers. Indi- vidual developers have a great opportunity to publish mobile applications on A the Android Market; in only five months’ time the number of applications has doubled, with over 20,000 available today. And the widespread use of Android has increased demand for corporate developers as companies are looking for a mobile presence. You can be part of this. n With real-world source code in hand, Pro Android 2 covers mobile application development for the Android platform from basic concepts such as Android Resources, Intents, and Content Providers to OpenGL, Text to Speech, Multi- touch, Home Screen Widgets, and Titanium Mobile. We teach you how to build d Android applications by taking you through Android APIs, from basic to ad- vanced, one step at a time. Android makes mobile programming far more accessible than any other r mobile platforms available today. At no cost to you, you can download the Eclipse IDE and the Android SDK, and you will have everything you need to o start writing great applications for Android mobile devices. You will not even need a physical device—the Android SDK provides an emulator that you can run on your desktop. i Pro Android 2 is the result of over two years of effort to bring together in one place everything you need—both basic and advanced—to be an Android d developer. So what are you waiting for? Pro 2 Android 2 s K o Mm Sayed Hashimi | Satya Komatineni | Dave MacLean H aaa cLtinsh COMPANION eBOOK SEE LAST PAGE FOR DETAILS ON $10 eBOOK VERSION ISBN 978-1-4302-2659-8 eanenim 54999 i i US $49.99 Shelve in Mobile Computing SOURCE CODE ONLINE User level: www.apress.com Intermediate-Advanced 9 781430 226598 this print for content only—size & color not accurate Trim: 7.5 x 9.25 spine = 1.375" 736 page count Pro Android 2 ■ ■ ■ Sayed Y. Hashimi Satya Komatineni Dave MacLean i Pro Android 2 Copyright © 2010 by Sayed Y. Hashimi, Satya Komatineni, and Dave MacLean 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-2659-8 ISBN-13 (electronic): 978-1-4302-2660-4 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. President and Publisher: Paul Manning Lead Editor: Steve Anglin Development Editor: Douglas Pundick Technical Reviewer: Vikram Goyal Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Fran Parnell Copy Editor: Elizabeth Berry Compositor: MacPS, LLC Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders- [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], 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. You will need to answer questions pertaining to this book in order to successfully download the code. ii To my son, Sayed-Adieb. —Sayed Y. Hashimi To my beautiful wife, AnnMarie, for her spirit; to Ashley, for her undaunting hope; to Nikolas, for his kindness; to Kavitha, for being smart, witty, and fabulous; to Narayan, for sheer cuteness; and to all my extended family in India and the USA for their love. —Satya Komatineni To my wife, Rosie, and my son, Mike, for their support; I couldn't have done this without them. And to Max, for spending so much time at my feet keeping me company. —Dave MacLean iii Contents at a Glance ■Contents at a Glance...................................................................................................iv ■Contents.......................................................................................................................v ■About the Authors.....................................................................................................xiii ■About the Technical Reviewer..................................................................................xiv ■Acknowledgments.....................................................................................................xv ■Foreword...................................................................................................................xvi ■Chapter 1: Introducing the Android Computing Platform...........................................1(cid:1) ■Chapter 2: Getting Your Feet Wet..............................................................................25(cid:1) ■Chapter 3: Using Resources, Content Providers, and Intents...................................57(cid:1) ■Chapter 4: Building User Interfaces and Using Controls.........................................123(cid:1) ■Chapter 5: Working with Menus and Dialogs..........................................................171(cid:1) ■Chapter 6: Unveiling 2D Animation.........................................................................217(cid:1) ■Chapter 7: Exploring Security and Location-Based Services..................................243(cid:1) ■Chapter 8: Building and Consuming Services.........................................................289(cid:1) ■Chapter 9: Using the Media Framework and Telephony APIs.................................327(cid:1) ■Chapter 10: Programming 3D Graphics with OpenGL.............................................363(cid:1) ■Chapter 11: Managing and Organizing Preferences...............................................421(cid:1) ■Chapter 12: Exploring Live Folders.........................................................................439(cid:1) ■Chapter 13: Home Screen Widgets.........................................................................457(cid:1) ■Chapter 14: Android Search....................................................................................491(cid:1) ■Chapter 15: Exploring Text to Speech and Translate APIs......................................563(cid:1) ■Chapter 16: Touchscreens......................................................................................591(cid:1) ■Chapter 17: Titanium Mobile: A WebKit-Based Approach to Android Development.......................................................................................627(cid:1) ■Chapter 18: Working with Android Market.............................................................661(cid:1) ■Chapter 19: Outlook and Resources.......................................................................675(cid:1) ■Index.......................................................................................................................687 iv Contents ■Contents at a Glance.......................................................................................iv(cid:1) ■Contents..........................................................................................................v(cid:1) ■About the Authors ........................................................................................xiii(cid:1) ■About the Technical Reviewer.......................................................................xiv(cid:1) ■Acknowledgments..........................................................................................xv(cid:1) ■Foreword.......................................................................................................xvi(cid:1) ■Chapter 1: Introducing the Android Computing Platform................................1(cid:1) A New Platform for a New Personal Computer.......................................................................................................1(cid:1) History of Android...................................................................................................................................................3(cid:1) Delving into the Dalvik VM......................................................................................................................................5(cid:1) Comparing Android and Java ME............................................................................................................................7(cid:1) Understanding the Android Software Stack..........................................................................................................10(cid:1) Developing an End-User Application with the Android SDK..................................................................................11(cid:1) Android Emulator.............................................................................................................................................11(cid:1) The Android UI.................................................................................................................................................12(cid:1) The Android Foundational Components...........................................................................................................13(cid:1) Advanced UI Concepts.....................................................................................................................................14(cid:1) Android Service Components...........................................................................................................................16(cid:1) Android Media and Telephony Components....................................................................................................16(cid:1) Android Java Packages....................................................................................................................................18(cid:1) Taking Advantage of Android Source Code...........................................................................................................23(cid:1) Summary..............................................................................................................................................................24(cid:1) ■Chapter 2: Getting Your Feet Wet..................................................................25(cid:1) Setting Up Your Environment................................................................................................................................25(cid:1) Downloading JDK 6..........................................................................................................................................26(cid:1) Downloading Eclipse 3.5..................................................................................................................................26 Downloading the Android SDK.........................................................................................................................27(cid:1) v ■ CONTENTS Installing Android Development Tools (ADT)....................................................................................................28(cid:1) Learning the Fundamental Components...............................................................................................................31(cid:1) View.................................................................................................................................................................31(cid:1) Activity.............................................................................................................................................................31(cid:1) Intent................................................................................................................................................................31(cid:1) Content Provider..............................................................................................................................................32(cid:1) Service.............................................................................................................................................................32(cid:1) AndroidManifest.xml........................................................................................................................................32(cid:1) Android Virtual Devices....................................................................................................................................32(cid:1) Hello World!..........................................................................................................................................................33(cid:1) Android Virtual Devices.........................................................................................................................................38(cid:1) Exploring the Structure of an Android Application................................................................................................40(cid:1) Analyzing the Notepad Application.......................................................................................................................42(cid:1) Loading and Running the Notepad Application................................................................................................43(cid:1) Dissecting the Application...............................................................................................................................44(cid:1) Examining the Application Lifecycle.....................................................................................................................51(cid:1) Debugging Your App.............................................................................................................................................54(cid:1) Summary..............................................................................................................................................................55(cid:1) ■Chapter 3: Using Resources, Content Providers, and Intents........................57(cid:1) Understanding Resources.....................................................................................................................................58(cid:1) String Resources..............................................................................................................................................59(cid:1) Layout Resources............................................................................................................................................60(cid:1) Resource-Reference Syntax............................................................................................................................62(cid:1) Defining Your Own Resource IDs for Later Use................................................................................................63(cid:1) Compiled and Noncompiled Android Resources..............................................................................................64(cid:1) Enumerating Key Android Resources...............................................................................................................65(cid:1) Working with Arbitrary XML Resource Files....................................................................................................73(cid:1) Working with Raw Resources..........................................................................................................................74(cid:1) Working with Assets........................................................................................................................................75(cid:1) Reviewing the Resources Directory Structure.................................................................................................75(cid:1) Understanding Content Providers.........................................................................................................................76(cid:1) Exploring Android’s Built-in Providers.............................................................................................................77(cid:1) Architecture of Content Providers....................................................................................................................83(cid:1) Implementing Content Providers.....................................................................................................................95(cid:1) Understanding Intents.........................................................................................................................................106(cid:1) Available Intents in Android...........................................................................................................................107(cid:1) Intents and Data URIs....................................................................................................................................109(cid:1) Generic Actions..............................................................................................................................................110(cid:1) Using Extra Information.................................................................................................................................111(cid:1) Using Components to Directly Invoke an Activity..........................................................................................113(cid:1) Best Practice for Component Designers........................................................................................................114(cid:1) Understanding Intent Categories...................................................................................................................114(cid:1) The Rules for Resolving Intents to Their Components...................................................................................117(cid:1) Exercising the ACTION_PICK..........................................................................................................................117(cid:1) Exercising the GET_CONTENT Action.............................................................................................................119(cid:1) Further Resources for This Chapter....................................................................................................................121(cid:1) Summary............................................................................................................................................................121(cid:1) vi ■ CONTENTS ■Chapter 4: Building User Interfaces and Using Controls.............................123(cid:1) UI Development in Android.................................................................................................................................123(cid:1) Understanding Android’s Common Controls.......................................................................................................129(cid:1) Text Controls..................................................................................................................................................129(cid:1) Button Controls..............................................................................................................................................133(cid:1) List Controls...................................................................................................................................................139(cid:1) Grid Controls..................................................................................................................................................143(cid:1) Date and Time Controls.................................................................................................................................145(cid:1) Other Interesting Controls in Android..................................................................................................................147(cid:1) The MapView Control.....................................................................................................................................148(cid:1) The Gallery Control.........................................................................................................................................148(cid:1) The Spinner Control.......................................................................................................................................148(cid:1) Understanding Layout Managers........................................................................................................................149(cid:1) The LinearLayout Layout Manager.................................................................................................................149(cid:1) The TableLayout Layout Manager..................................................................................................................153(cid:1) The RelativeLayout Layout Manager..............................................................................................................157(cid:1) The FrameLayout Layout Manager................................................................................................................159(cid:1) Customizing Layout for Various Device Configurations.................................................................................162(cid:1) Understanding Adapters.....................................................................................................................................164(cid:1) Getting to Know SimpleCursorAdapter..........................................................................................................165(cid:1) Getting to Know ArrayAdapter.......................................................................................................................165(cid:1) Creating Custom Adapters.............................................................................................................................166(cid:1) Debugging and Optimizing Layouts with the Hierarchy Viewer..........................................................................167(cid:1) Summary............................................................................................................................................................170(cid:1) ■Chapter 5: Working with Menus and Dialogs..............................................171(cid:1) Understanding Android Menus...........................................................................................................................171(cid:1) Creating a Menu.............................................................................................................................................173(cid:1) Working with Menu Groups...........................................................................................................................174(cid:1) Responding to Menu Items............................................................................................................................175(cid:1) Creating a Test Harness for Testing Menus...................................................................................................176(cid:1) Working with Other Menu Types.........................................................................................................................183(cid:1) Expanded Menus...........................................................................................................................................183(cid:1) Working with Icon Menus..............................................................................................................................183(cid:1) Working with Submenus................................................................................................................................184(cid:1) Provisioning for System Menus.....................................................................................................................185(cid:1) Working with Context Menus.........................................................................................................................185(cid:1) Working with Alternative Menus....................................................................................................................188(cid:1) Working with Menus in Response to Changing Data.....................................................................................192(cid:1) Loading Menus Through XML Files.....................................................................................................................192(cid:1) Structure of an XML Menu Resource File......................................................................................................193(cid:1) Inflating XML Menu Resource Files...............................................................................................................193(cid:1) Responding to XML-Based Menu Items.........................................................................................................194(cid:1) A Brief Introduction to Additional XML Menu Tags........................................................................................195(cid:1) Using Dialogs in Android.....................................................................................................................................196(cid:1) Designing an Alert Dialog..............................................................................................................................197(cid:1) Designing a Prompt Dialog............................................................................................................................199(cid:1) The Nature of Dialogs in Android...................................................................................................................204(cid:1) vii ■ CONTENTS Rearchitecting the Prompt Dialog..................................................................................................................205(cid:1) Working with Managed Dialogs..........................................................................................................................206(cid:1) Understanding the Managed-Dialog Protocol................................................................................................206(cid:1) Recasting the Non-Managed Dialog As a Managed Dialog............................................................................206(cid:1) Simplifying the Managed-Dialog Protocol.....................................................................................................208(cid:1) Summary............................................................................................................................................................215(cid:1) ■Chapter 6: Unveiling 2D Animation..............................................................217(cid:1) Frame-by-Frame Animation................................................................................................................................218(cid:1) Planning for Frame-by-Frame Animation......................................................................................................218(cid:1) Creating the Activity.......................................................................................................................................219(cid:1) Adding Animation to the Activity....................................................................................................................220(cid:1) Layout Animation................................................................................................................................................223(cid:1) Basic Tweening Animation Types..................................................................................................................224(cid:1) Planning the Layout-Animation Test Harness................................................................................................225(cid:1) Creating the Activity and the ListView...........................................................................................................226(cid:1) Animating the ListView..................................................................................................................................228(cid:1) Using Interpolators.........................................................................................................................................231(cid:1) View Animation...................................................................................................................................................233(cid:1) Understanding View Animation......................................................................................................................233(cid:1) Adding Animation...........................................................................................................................................236(cid:1) Using Camera to Provide Depth Perception in 2D..........................................................................................239(cid:1) Exploring the AnimationListener Class..........................................................................................................240(cid:1) Some Notes on Transformation Matrices......................................................................................................241(cid:1) Summary............................................................................................................................................................242(cid:1) ■Chapter 7: Exploring Security and Location-Based Services......................243(cid:1) Understanding the Android Security Model........................................................................................................243(cid:1) Overview of Security Concepts......................................................................................................................244(cid:1) Signing Applications for Deployment.............................................................................................................244(cid:1) Performing Runtime Security Checks.................................................................................................................249(cid:1) Understanding Security at the Process Boundary.........................................................................................249(cid:1) Declaring and Using Permissions..................................................................................................................250(cid:1) Understanding and Using Custom Permissions.............................................................................................252(cid:1) Understanding and Using URI Permissions....................................................................................................258(cid:1) Working with Location-Based Services..............................................................................................................258(cid:1) Understanding the Mapping Package............................................................................................................259(cid:1) Understanding the Location Package............................................................................................................270(cid:1) Summary............................................................................................................................................................287(cid:1) ■Chapter 8: Building and Consuming Services.............................................289(cid:1) Consuming HTTP Services..................................................................................................................................289(cid:1) Using the HttpClient for HTTP GET Requests.................................................................................................290(cid:1) Using the HttpClient for HTTP POST Requests...............................................................................................291(cid:1) Dealing with Exceptions.................................................................................................................................295(cid:1) Addressing Multithreading Issues.................................................................................................................297(cid:1) Doing Interprocess Communication....................................................................................................................301(cid:1) Creating a Simple Service..............................................................................................................................301(cid:1) Understanding Services in Android................................................................................................................302(cid:1) Understanding Local Services.......................................................................................................................303(cid:1) viii
Description: