CYAN YELLOW SPOT MATTE MAGENTA BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS® Companion eBook Available Begin the journey toward your own First, welcome to the world of Android! As you have probably noticed, Android is rapidly growing; with the release of Android 2, many devices B successful Android 2 applications from various manufacturers are dotting the landscape. With millions of de- e vices in the wild, and millions more on the way, those who get into the g latest version of Android early will reap the benefits as the ecosystem con- i n tinues to grow. n Second, welcome to the new edition of this book, which now covers Android i 2! Its purpose is to start you on your way to building compelling Android 2 ap- n plications, and to help you master the learning curve. Android 2 is a rich frame- g work, which means that there is a lot of cool stuff for you to pick up along your journey in order to create the slickest, most useful apps you can imagine. AA Android 2 is an important release of the framework, so it’s important to get off to the best possible start when learning the ins and outs of Android 2 nn development. This book guides you on your journey to learning Android 2, pointing out what is possible and showing you how to turn those concepts dd into reality. From there, building practical and popular apps is a matter of time and creativity. rr oo ii dd 22 Beginning Android 2 M Mark L. Murphy u r p COMPANION eBOOK SEE LAST PAGE FOR DETAILS ON $10 eBOOK VERSION IISSBBNN 997788--11--44330022--22662299--11 hy 5544449999 US $44.99 Shelve in Mobile Computing SOURCE CODE ONLINE User level: www.apress.com Beginner 99 778811443300 222266229911 this print for content only—size & color not accurate Trim: 7.5 x 9.25 spine = 0.78125" 416 page count Beginning Android 2 ■ ■ ■ Mark L. Murphy i Beginning Android 2 Copyright © 2010 by Mark L. Murphy 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-2629-1 ISBN-13 (electronic): 978-1-4302-2630-7 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. Java™ and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc., in the US and other countries. Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book was written without endorsement from Sun Microsystems, Inc. President and Publisher: Paul Manning Lead Editor: Steve Anglin Development Editor: Matthew Moodie 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: Marilyn S. Smith Compositor: MacPS, LLC Indexer: John Collin 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. ii iii Contents at a Glance ■Contents at a Glance.................................................................................................................................iv ■Contents.....................................................................................................................................................v ■About the Author.....................................................................................................................................xiii ■Acknowledgments...................................................................................................................................xiv ■Preface......................................................................................................................................................xv ■Chapter 1: The Big Picture.........................................................................................................................1(cid:1) ■Chapter 2: Projects and Targets................................................................................................................5(cid:1) ■Chapter 3: Creating a Skeleton Application.............................................................................................17(cid:1) ■Chapter 4: Using XML-Based Layouts......................................................................................................23(cid:1) ■Chapter 5: Employing Basic Widgets.......................................................................................................29(cid:1) ■Chapter 6: Working with Containers........................................................................................................39(cid:1) ■Chapter 7: Using Selection Widgets.........................................................................................................59(cid:1) ■Chapter 8: Getting Fancy with Lists.........................................................................................................75(cid:1) ■Chapter 9: Employing Fancy Widgets and Containers.............................................................................95(cid:1) ■Chapter 10: The Input Method Framework............................................................................................117(cid:1) ■Chapter 11: Applying Menus..................................................................................................................125(cid:1) ■Chapter 12: Fonts...................................................................................................................................137(cid:1) ■Chapter 13: Embedding the WebKit Browser.........................................................................................141(cid:1) ■Chapter 14: Showing Pop-Up Messages................................................................................................149(cid:1) ■Chapter 15: Dealing with Threads.........................................................................................................155(cid:1) ■Chapter 16: Handling Activity Life Cycle Events....................................................................................167(cid:1) ■Chapter 17: Creating Intent Filters........................................................................................................171(cid:1) ■Chapter 18: Launching Activities and Subactivities..............................................................................177(cid:1) ■Chapter 19: Handling Rotation...............................................................................................................185(cid:1) ■Chapter 20: Working with Resources....................................................................................................197(cid:1) ■Chapter 21: Using Preferences..............................................................................................................213(cid:1) ■Chapter 22: Managing and Accessing Local Databases........................................................................225(cid:1) ■Chapter 23: Accessing Files...................................................................................................................237(cid:1) ■Chapter 24: Leveraging Java Libraries..................................................................................................245(cid:1) ■Chapter 25: Communicating via the Internet.........................................................................................253(cid:1) ■Chapter 26: Using a Content Provider....................................................................................................259(cid:1) ■Chapter 27: Building a Content Provider...............................................................................................265(cid:1) ■Chapter 28: Requesting and Requiring Permissions.............................................................................275(cid:1) ■Chapter 29: Creating a Service..............................................................................................................279(cid:1) ■Chapter 30: Invoking a Service..............................................................................................................285(cid:1) ■Chapter 31: Alerting Users via Notifications..........................................................................................289(cid:1) ■Chapter 32: Accessing Location-Based Services...................................................................................295(cid:1) ■Chapter 33: Mapping with MapView and MapActivity...........................................................................301(cid:1) ■Chapter 34: Handling Telephone Calls...................................................................................................311(cid:1) ■Chapter 35: Development Tools.............................................................................................................315(cid:1) ■Chapter 36: Handling Multiple Screen Sizes..........................................................................................331(cid:1) ■Chapter 37: Dealing with Devices..........................................................................................................353(cid:1) ■Chapter 38: Handling Platform Changes................................................................................................359(cid:1) ■Chapter 39: Where Do We Go from Here?..............................................................................................367 ■Index......................................................................................................................................................369 iv Contents ■Contents at a Glance.......................................................................................iv(cid:1) ■Contents..........................................................................................................v(cid:1) ■About the Author..........................................................................................xiii(cid:1) ■Acknowledgments.........................................................................................xiv(cid:1) ■Preface...........................................................................................................xv(cid:1) ■Chapter 1: The Big Picture...............................................................................1(cid:1) Challenges of Smartphone Programming...............................................................................................................1(cid:1) What Androids Are Made Of....................................................................................................................................2(cid:1) Stuff at Your Disposal.............................................................................................................................................3(cid:1) ■Chapter 2: Projects and Targets......................................................................5(cid:1) Pieces and Parts.....................................................................................................................................................5(cid:1) Creating a Project...................................................................................................................................................6(cid:1) Project Structure.....................................................................................................................................................6(cid:1) Root Contents....................................................................................................................................................6(cid:1) The Sweat Off Your Brow...................................................................................................................................7(cid:1) And Now, the Rest of the Story..........................................................................................................................7(cid:1) What You Get Out of It........................................................................................................................................8(cid:1) Inside the Manifest.................................................................................................................................................8(cid:1) In the Beginning, There Was the Root, And It Was Good...................................................................................9(cid:1) Permissions, Instrumentations, and Applications (Oh My!)...............................................................................9(cid:1) Your Application Does Something, Right?.......................................................................................................10(cid:1) Achieving the Minimum...................................................................................................................................11(cid:1) Version=Control...............................................................................................................................................12(cid:1) Emulators and Targets..........................................................................................................................................12(cid:1) Virtually There..................................................................................................................................................12(cid:1) Aiming at a Target...........................................................................................................................................14(cid:1) (cid:1) v ■ CONTENTS ■Chapter 3: Creating a Skeleton Application...................................................17(cid:1) Begin at the Beginning..........................................................................................................................................17(cid:1) Dissecting the Activity..........................................................................................................................................18(cid:1) Building and Running the Activity.........................................................................................................................20(cid:1) ■Chapter 4: Using XML-Based Layouts ...........................................................23(cid:1) What Is an XML-Based Layout?............................................................................................................................23(cid:1) Why Use XML-Based Layouts?.............................................................................................................................24(cid:1) OK, So What Does It Look Like?............................................................................................................................24(cid:1) What’s with the @ Signs?.....................................................................................................................................25(cid:1) And How Do We Attach These to the Java?..........................................................................................................25(cid:1) The Rest of the Story............................................................................................................................................26(cid:1) ■Chapter 5: Employing Basic Widgets.............................................................29(cid:1) Assigning Labels...................................................................................................................................................29(cid:1) Button, Button, Who’s Got the Button?.................................................................................................................30(cid:1) Fleeting Images....................................................................................................................................................31(cid:1) Fields of Green. Or Other Colors...........................................................................................................................32(cid:1) Just Another Box to Check....................................................................................................................................33(cid:1) Turn the Radio Up.................................................................................................................................................35(cid:1) It’s Quite a View....................................................................................................................................................37(cid:1) Useful Properties.............................................................................................................................................37(cid:1) Useful Methods................................................................................................................................................38(cid:1) Colors...............................................................................................................................................................38(cid:1) ■Chapter 6: Working with Containers.............................................................39(cid:1) Thinking Linearly..................................................................................................................................................39(cid:1) LinearLayout Concepts and Properties............................................................................................................40(cid:1) LinearLayout Example......................................................................................................................................42(cid:1) All Things Are Relative..........................................................................................................................................46(cid:1) RelativeLayout Concepts and Properties.........................................................................................................47(cid:1) RelativeLayout Example...................................................................................................................................49(cid:1) Tabula Rasa..........................................................................................................................................................51(cid:1) TableLayout Concepts and Properties.............................................................................................................52(cid:1) TableLayout Example.......................................................................................................................................53(cid:1) Scrollwork.............................................................................................................................................................54(cid:1) ■Chapter 7: Using Selection Widgets..............................................................59(cid:1) Adapting to the Circumstances.............................................................................................................................59(cid:1) Lists of Naughty and Nice.....................................................................................................................................60(cid:1) Spin Control..........................................................................................................................................................63(cid:1) Grid Your Lions (or Something Like That...)..........................................................................................................66(cid:1) Fields: Now with 35% Less Typing!......................................................................................................................69(cid:1) Galleries, Give or Take the Art..............................................................................................................................72(cid:1) ■Chapter 8: Getting Fancy with Lists..............................................................75(cid:1) Getting to First Base.............................................................................................................................................75(cid:1) A Dynamic Presentation........................................................................................................................................77(cid:1) Better. Stronger. Faster........................................................................................................................................80(cid:1) Using convertView...........................................................................................................................................80(cid:1) Using the Holder Pattern..................................................................................................................................81(cid:1) vi ■ CONTENTS Making a List........................................................................................................................................................84(cid:1) ...And Checking It Twice.......................................................................................................................................89(cid:1) Adapting Other Adapters.......................................................................................................................................94(cid:1) ■Chapter 9: Employing Fancy Widgets and Containers...................................95(cid:1) Pick and Choose...................................................................................................................................................95(cid:1) Time Keeps Flowing Like a River..........................................................................................................................99(cid:1) Making Progress.................................................................................................................................................101(cid:1) Seeking Resolution.............................................................................................................................................101(cid:1) Put It on My Tab..................................................................................................................................................102(cid:1) The Pieces.....................................................................................................................................................103(cid:1) The Idiosyncrasies.........................................................................................................................................103(cid:1) Wiring It Together..........................................................................................................................................104(cid:1) Adding Them Up.............................................................................................................................................106(cid:1) Intents and Views..........................................................................................................................................109(cid:1) Flipping Them Off................................................................................................................................................109(cid:1) Manual Flipping.............................................................................................................................................110(cid:1) Adding Contents on the Fly............................................................................................................................112(cid:1) Automatic Flipping.........................................................................................................................................113(cid:1) Getting in Someone’s Drawer.............................................................................................................................114(cid:1) Other Good Stuff.................................................................................................................................................116(cid:1) ■Chapter 10: The Input Method Framework..................................................117(cid:1) Keyboards, Hard and Soft...................................................................................................................................117(cid:1) Tailored to Your Needs........................................................................................................................................118(cid:1) Tell Android Where It Can Go..............................................................................................................................121(cid:1) Fitting In..............................................................................................................................................................123(cid:1) Unleash Your Inner Dvorak.................................................................................................................................124(cid:1) ■Chapter 11: Applying Menus.......................................................................125(cid:1) Menus of Options................................................................................................................................................125(cid:1) Creating an Options Menu.............................................................................................................................125(cid:1) Adding Menu Choices and Submenus...........................................................................................................126(cid:1) Menus in Context................................................................................................................................................127(cid:1) Taking a Peek.....................................................................................................................................................128(cid:1) Yet More Inflation................................................................................................................................................133(cid:1) Menu XML Structure......................................................................................................................................133(cid:1) Menu Options and XML..................................................................................................................................134(cid:1) Inflating the Menu..........................................................................................................................................135(cid:1) ■Chapter 12: Fonts........................................................................................137(cid:1) Love the One You’re With....................................................................................................................................137(cid:1) More Fonts..........................................................................................................................................................138(cid:1) Here a Glyph, There a Glyph...............................................................................................................................140(cid:1) ■Chapter 13: Embedding the WebKit Browser..............................................141(cid:1) A Browser, Writ Small.........................................................................................................................................141(cid:1) Loading It Up.......................................................................................................................................................143(cid:1) Navigating the Waters.........................................................................................................................................144(cid:1) Entertaining the Client........................................................................................................................................145(cid:1) Settings, Preferences, and Options (Oh My!)......................................................................................................147(cid:1) vii ■ CONTENTS ■Chapter 14: Showing Pop-Up Messages......................................................149(cid:1) Raising Toasts.....................................................................................................................................................149(cid:1) Alert! Alert!..........................................................................................................................................................150(cid:1) Checking Them Out.............................................................................................................................................151(cid:1) ■Chapter 15: Dealing with Threads...............................................................155(cid:1) Getting Through the Handlers.............................................................................................................................155(cid:1) Messages.......................................................................................................................................................156(cid:1) Runnables......................................................................................................................................................158(cid:1) Running in Place.................................................................................................................................................159(cid:1) Where Oh Where Has My UI Thread Gone?.........................................................................................................159(cid:1) Asyncing Feeling.................................................................................................................................................159(cid:1) The Theory.....................................................................................................................................................159(cid:1) AsyncTask, Generics, and Varargs.................................................................................................................160(cid:1) The Stages of AsyncTask...............................................................................................................................160(cid:1) A Sample Task...............................................................................................................................................161(cid:1) And Now, the Caveats.........................................................................................................................................164(cid:1) ■Chapter 16: Handling Activity Life Cycle Events..........................................167(cid:1) Schroedinger’s Activity.......................................................................................................................................167(cid:1) Life, Death, and Your Activity..............................................................................................................................168(cid:1) onCreate() and onDestroy()............................................................................................................................168(cid:1) onStart(), onRestart(), and onStop()...............................................................................................................169(cid:1) onPause() and onResume()............................................................................................................................169(cid:1) The Grace of State..............................................................................................................................................169(cid:1) ■Chapter 17: Creating Intent Filters..............................................................171(cid:1) What’s Your Intent?.............................................................................................................................................171(cid:1) Pieces of Intents............................................................................................................................................172(cid:1) Intent Routing................................................................................................................................................172(cid:1) Stating Your Intent(ions).....................................................................................................................................173(cid:1) Narrow Receivers...............................................................................................................................................174(cid:1) The Pause Caveat...............................................................................................................................................175(cid:1) ■Chapter 18: Launching Activities and Subactivities....................................177(cid:1) Peers and Subs...................................................................................................................................................177(cid:1) Start ’Em Up........................................................................................................................................................178(cid:1) Make an Intent...............................................................................................................................................178(cid:1) Make the Call.................................................................................................................................................178(cid:1) Tabbed Browsing, Sort Of...................................................................................................................................182(cid:1) ■Chapter 19: Handling Rotation....................................................................185(cid:1) A Philosophy of Destruction................................................................................................................................185(cid:1) It’s All the Same, Just Different..........................................................................................................................185(cid:1) Now with More Savings!.....................................................................................................................................189(cid:1) DIY Rotation........................................................................................................................................................191(cid:1) Forcing the Issue................................................................................................................................................193(cid:1) Making Sense of It All.........................................................................................................................................195(cid:1) ■Chapter 20: Working with Resources..........................................................197(cid:1) The Resource Lineup..........................................................................................................................................197(cid:1) String Theory......................................................................................................................................................198(cid:1) viii
Description: