ebook img

Android Recipes: A Problem-Solution Approach PDF

952 Pages·2012·9.042 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Android Recipes: A Problem-Solution Approach

Android Recipes A Problem-Solution Approach (cid:132) (cid:132) (cid:132) Dave Smith Jeff Friesen Apress i Android Recipes: A Problem-Solution Approach Copyright © 2012 by Dave Smith and Jeff Friesen This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher's location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. ISBN978-1-4302-4614-5 ISBN 978-1-4302-4615-2 (eBook) Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The images of the Android Robot (01 / Android Robot) are reproduced from work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License. Android and all Android and Google-based marks are trademarks or registered trademarks of Google, Inc., in the U.S. and other countries. Apress Media, L.L.C. is not affiliated with Google, Inc., and this book was written without endorsement from Google, Inc. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. President and Publisher: Paul Manning Lead Editor: Steve Anglin Developmental Editor: Tom Welsh Technical Reviewer: Chád Darby Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Louise Corrigan, Morgan Ertel, Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Tom Welsh Coordinating Editor: Jill Balzano Copy Editor: Christine Dahlin Compositor: Bytheway Publishing Services Indexer: SPi Global Artist: SPi Global Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [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/bulk-sales. Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code. Contents at a Glance (cid:132) Foreword...................................................................................................xviii (cid:132) About the Authors........................................................................................xix (cid:132) About the Technical Reviewer......................................................................xx (cid:132) Acknowledgments.......................................................................................xxi (cid:132) Preface.......................................................................................................xxii (cid:132) Chapter 1: Getting Started with Android............................................... 1 (cid:132) Chapter 2: User Interface Recipes....................................................... 99 (cid:132) Chapter 3: Communications and Networking................................... 323 (cid:132) Chapter 4: Interacting with Device Hardware and Media................. 421 (cid:132) Chapter 5: Persisting Data................................................................ 501 (cid:132) Chapter 6: Interacting with the System............................................ 581 (cid:132) Chapter 7: Working with Libraries.................................................... 689 (cid:132) Chapter 8: Working with Android NDK and Renderscript................. 743 (cid:132) Appendix A: Scripting Layer for Android........................................... 805 (cid:132) Appendix B: Android Tools Overview................................................ 821 (cid:132) Appendix C: App Design Guidelines .................................................. 855 (cid:132) Appendix D: Univerter Architecture.................................................. 867 (cid:132) Index.................................................................................................. 911 iii Contents (cid:132) Foreword...................................................................................................xviii (cid:132) About the Authors........................................................................................xix (cid:132) About the Technical Reviewer......................................................................xx (cid:132) Acknowledgments.......................................................................................xxi (cid:132) Preface.......................................................................................................xxii (cid:132) Chapter 1: Getting Started with Android............................................... 1 What Is Android?............................................................................................................1 History of Android..........................................................................................................2 Android Architecture......................................................................................................4 App Architecture............................................................................................................9 Components............................................................................................................................................................9 Resources.............................................................................................................................................................34 Manifest................................................................................................................................................................46 App Package.........................................................................................................................................................50 Installing the Android SDK....................................................................................................................................51 Installing an Android Platform..............................................................................................................................55 Creating an Android Virtual Device.......................................................................................................................60 Starting the AVD...................................................................................................................................................64 Introducing Univerter............................................................................................................................................71 Creating Univerter.................................................................................................................................................73 Installing and Running Univerter..........................................................................................................................76 Preparing Univerter for Publication on Google Play..............................................................................................81 Migrating to Eclipse..............................................................................................................................................88 Creating and Running Univerter with Eclipse.......................................................................................................92 Summary......................................................................................................................97 (cid:132) Chapter 2: User Interface Recipes....................................................... 99 2-1. Customizing the Window......................................................................................99 Problem................................................................................................................................................................99 Solution................................................................................................................................................................99 How It Works......................................................................................................................................................100 iv CONTENTS 2-2. Creating and Displaying Views...........................................................................112 Problem..............................................................................................................................................................112 Solution..............................................................................................................................................................112 How It Works......................................................................................................................................................113 2-3. Monitoring Click Actions.....................................................................................115 Problem..............................................................................................................................................................115 Solution..............................................................................................................................................................115 How It Works......................................................................................................................................................115 2-4. Resolution-Independent Assets..........................................................................117 Problem..............................................................................................................................................................117 Solution..............................................................................................................................................................117 How It Works......................................................................................................................................................118 2-5. Locking Activity Orientation................................................................................119 Problem..............................................................................................................................................................119 Solution..............................................................................................................................................................119 How It Works......................................................................................................................................................120 2-6. Dynamic Orientation Locking..............................................................................120 Problem..............................................................................................................................................................120 Solution..............................................................................................................................................................121 How It Works......................................................................................................................................................121 2-7. Manually Handling Rotation................................................................................123 Problem..............................................................................................................................................................123 Solution..............................................................................................................................................................123 How It Works......................................................................................................................................................124 2-8. Creating Pop-Up Menu Actions...........................................................................126 Problem..............................................................................................................................................................126 Solution..............................................................................................................................................................126 How It Works......................................................................................................................................................126 2-9. Displaying A User Dialog.....................................................................................132 Problem..............................................................................................................................................................132 Solution..............................................................................................................................................................133 How It Works......................................................................................................................................................133 2-10. Customizing Options Menu...............................................................................138 Problem..............................................................................................................................................................138 Solution..............................................................................................................................................................138 How It Works......................................................................................................................................................139 2-11. Customizing Back Behavior..............................................................................143 Problem..............................................................................................................................................................143 Solution..............................................................................................................................................................143 How It Works......................................................................................................................................................143 2-12. Emulating the HOME Button.............................................................................147 v CONTENTS Problem..............................................................................................................................................................147 Solution..............................................................................................................................................................147 How It Works......................................................................................................................................................147 2-13. Monitoring TextView Changes..........................................................................148 Problem..............................................................................................................................................................148 Solution..............................................................................................................................................................148 How It Works......................................................................................................................................................148 2-14. Scrolling TextView Ticker.................................................................................151 Problem..............................................................................................................................................................151 Solution..............................................................................................................................................................151 How It Works......................................................................................................................................................152 2-15. Animating a View..............................................................................................153 Problem..............................................................................................................................................................153 Solution..............................................................................................................................................................153 How It Works......................................................................................................................................................153 2-16. Animating Layout Changes...............................................................................166 Problem..............................................................................................................................................................166 Solution..............................................................................................................................................................166 How It Works......................................................................................................................................................166 2-17. Creating Drawables as Backgrounds...............................................................170 Problem..............................................................................................................................................................170 Solution..............................................................................................................................................................170 How It Works......................................................................................................................................................171 2-18. Creating Custom State Drawables....................................................................178 Problem..............................................................................................................................................................178 Solution..............................................................................................................................................................179 How It Works......................................................................................................................................................179 2-19. Applying Masks to Images................................................................................181 Problem..............................................................................................................................................................181 Solution..............................................................................................................................................................181 How It Works......................................................................................................................................................182 2-20. Creating Dialogs That Persist...........................................................................186 Problem..............................................................................................................................................................186 Solution..............................................................................................................................................................187 How It Works......................................................................................................................................................187 2-21. Implementing Situation-Specific Layouts.........................................................189 Problem..............................................................................................................................................................189 Solution..............................................................................................................................................................189 How It Works......................................................................................................................................................189 vi CONTENTS 2-22. Customizing Keyboard Actions.........................................................................199 Problem..............................................................................................................................................................199 Solution..............................................................................................................................................................199 How It Works......................................................................................................................................................199 2-23. Dismissing Soft Keyboard.................................................................................202 Problem..............................................................................................................................................................202 Solution..............................................................................................................................................................203 How It Works......................................................................................................................................................203 2-24. Customizing AdapterView Empty Views...........................................................203 Problem..............................................................................................................................................................203 Solution..............................................................................................................................................................203 How It Works......................................................................................................................................................204 2-25. Customizing ListView Rows..............................................................................205 Problem..............................................................................................................................................................205 Solution..............................................................................................................................................................206 How It Works......................................................................................................................................................206 2-26. Making ListView Section Headers....................................................................210 Problem..............................................................................................................................................................210 Solution..............................................................................................................................................................210 How It Works......................................................................................................................................................211 2-27. Creating Compound Controls............................................................................214 Problem..............................................................................................................................................................214 Solution..............................................................................................................................................................214 How It Works......................................................................................................................................................214 2–28. Handling Complex Touch Events......................................................................218 Problem..............................................................................................................................................................218 Solution..............................................................................................................................................................218 How It Works......................................................................................................................................................220 2-29. Forwarding Touch Events.................................................................................238 Problem..............................................................................................................................................................238 Solution..............................................................................................................................................................238 How It Works......................................................................................................................................................238 2-30. Making Drag-and-Drop Views..........................................................................243 Problem..............................................................................................................................................................243 Solution..............................................................................................................................................................243 How It Works......................................................................................................................................................245 2-31. Customizing Transition Animations..................................................................252 Problem..............................................................................................................................................................252 Solution..............................................................................................................................................................252 How It Works......................................................................................................................................................252 vii CONTENTS 2-32. Creating View Transformations........................................................................263 Problem..............................................................................................................................................................263 Solution..............................................................................................................................................................263 How It Works......................................................................................................................................................263 2-33. Swiping Between Views...................................................................................270 Problem..............................................................................................................................................................270 Solution..............................................................................................................................................................270 How It Works......................................................................................................................................................270 2-34. Creating Modular Interfaces.............................................................................281 Problem..............................................................................................................................................................281 Solution..............................................................................................................................................................281 How It Works......................................................................................................................................................282 2-35. High-Performance Drawing..............................................................................293 Problem..............................................................................................................................................................293 Solution..............................................................................................................................................................293 How It Works......................................................................................................................................................294 Useful Tools to Know: Hierarchy Viewer and Lint......................................................305 Hierarchy Viewer........................................................................................................305 Running Hierarchy Viewer..................................................................................................................................306 Exploring the View Hierarchy Window.......................................................................308 Working with an Individual View in Tree View...........................................................311 Debugging with View Hierarchy.................................................................................312 Optimizing with View Hierarchy.........................................................................................................................313 Exploring the Pixel Perfect Window...........................................................................313 Working with Pixel Perfect Overlays..........................................................................315 Lint.............................................................................................................................316 Running Lint...............................................................................................................318 Summary....................................................................................................................321 (cid:132) Chapter 3: Communications and Networking................................... 323 3–1. Displaying Web Information...............................................................................323 Problem..............................................................................................................................................................323 Solution..............................................................................................................................................................323 How It Works......................................................................................................................................................324 3–2. Intercepting WebView Events............................................................................328 Problem..............................................................................................................................................................328 Solution..............................................................................................................................................................329 How It Works......................................................................................................................................................329 3–3. Accessing WebView with JavaScript.................................................................331 Problem..............................................................................................................................................................331 viii CONTENTS Solution..............................................................................................................................................................331 How It Works......................................................................................................................................................331 3–4. Downloading an Image File................................................................................334 Problem..............................................................................................................................................................334 Solution..............................................................................................................................................................334 How It Works......................................................................................................................................................334 3–5. Downloading Completely in the Background.....................................................337 Problem..............................................................................................................................................................337 Solution..............................................................................................................................................................337 How It Works......................................................................................................................................................338 3–6. Accessing a REST API........................................................................................341 Problem..............................................................................................................................................................341 Solution..............................................................................................................................................................342 How It Works......................................................................................................................................................343 3–7. Parsing JSON.....................................................................................................368 Problem..............................................................................................................................................................368 Solution..............................................................................................................................................................368 How It Works......................................................................................................................................................368 3–8. Parsing XML.......................................................................................................372 Problem..............................................................................................................................................................372 Solution..............................................................................................................................................................372 How It Works......................................................................................................................................................372 3–9. Receiving SMS...................................................................................................383 Problem..............................................................................................................................................................383 Solution..............................................................................................................................................................383 How It Works......................................................................................................................................................383 3–10. Sending an SMS Message...............................................................................385 Problem..............................................................................................................................................................385 Solution..............................................................................................................................................................385 How It Works......................................................................................................................................................385 3–11. Communicating over Bluetooth........................................................................387 Problem..............................................................................................................................................................387 Solution..............................................................................................................................................................388 How It Works......................................................................................................................................................388 3–12. Querying Network Reachability........................................................................398 Problem..............................................................................................................................................................398 Solution..............................................................................................................................................................398 How It Works......................................................................................................................................................399 3-13. Transferring Data with NFC..............................................................................400 Problem..............................................................................................................................................................400 Solution..............................................................................................................................................................401 ix

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.