Ent Book: Very Well Written, Easy to Read, and Fun
Total Page:16
File Type:pdf, Size:1020Kb
Prepared exclusively for Antonio Pardo What readers are saying about Hello, Android This is a most excellent book: very well written, easy to read, and fun. In addition, any of Android’s quirks are explained along with just the right amount of detail to ensure quality programming principles are followed. Anthony Stevens Founder and CTO, PocketJourney and Top 20 Winner of Google Android Competition Ed Burnette covers an impressive amount of ground in a nicely com- pact book while retaining the popular Pragmatic style. For the mate- rial on 2D and 3D graphics alone, this is worthy of a spot in any Android developer’s library. Mark Murphy Founder, CommonsWare I remember when I first started to work with Android; it was like a huge maze. With this book, the introduction would have been much less painful. I am convinced that by reading this book new Android programmers will have an easier start. Gabor Paller Senior Software Architect, OnRelay, Ltd. Prepared exclusively for Antonio Pardo Hello, Android Introducing Google’s Mobile Development Platform Ed Burnette The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas Prepared exclusively for Antonio Pardo Many of the designations used by manufacturers and sellers to distinguish their prod- ucts are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC. Portions of the book’s cover are reproduced from work created and shared by Google and used according to terms described in the Creative Commons 2.5 Attribution License. See http://code.google.com/policies.html#restrictions for details. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://www.pragprog.com Copyright © 2008 Ed Burnette. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmit- ted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-10: 1-934356-17-4 ISBN-13: 978-1-934356-17-3 Printed on acid-free paper. P1.1 printing, May 26, 2008 Version: 2009-5-26 Prepared exclusively for Antonio Pardo Contents Acknowledgments 9 Changes (Cupcake Updates) 10 P1.1—May 26 ............................ 10 Preface 12 What Makes Android Special? ................... 12 Who Should Read This Book? ................... 13 What’s in This Book? ........................ 14 What’s New for Cupcake? ..................... 14 Online Resources .......................... 15 Fast-Forward >> ........................... 15 I Introducing Android 17 1 Quick Start 18 1.1 Installing the Tools ..................... 18 1.2 Creating Your First Program ................ 22 1.3 Running on the Emulator ................. 22 1.4 Running on a Real Phone ................. 27 1.5 Fast-Forward >> ....................... 27 2 Key Concepts 28 2.1 The Big Picture ....................... 28 2.2 It’s Alive! ........................... 33 2.3 Building Blocks ....................... 37 2.4 Using Resources ....................... 38 2.5 Safe and Secure ....................... 39 2.6 Fast-Forward >> ....................... 40 Prepared exclusively for Antonio Pardo CONTENTS 6 II Android Basics 41 3 Designing the User Interface 42 3.1 Introducing the Sudoku Example ............. 42 3.2 Designing by Declaration .................. 43 3.3 Creating the Opening Screen ............... 44 3.4 Using Alternate Resources ................. 52 3.5 Implementing an About Box ................ 55 3.6 Applying a Theme ...................... 60 3.7 Adding a Menu ....................... 61 3.8 Adding Settings ....................... 64 3.9 Starting a New Game .................... 66 3.10 Debugging with Log Messages ............... 68 3.11 Debugging with the Debugger ............... 69 3.12 Exiting the Game ...................... 69 3.13 Fast-Forward >> ....................... 70 4 Exploring 2D Graphics 71 4.1 Learning the Basics ..................... 71 4.2 Adding Graphics to Sudoku ................ 76 4.3 Handling Input ....................... 85 4.4 The Rest of the Story .................... 91 4.5 Making More Improvements ................ 100 4.6 Fast-Forward >> ....................... 101 5 Multimedia 102 5.1 Playing Audio ........................ 102 5.2 Playing Video ......................... 108 5.3 Adding Sounds to Sudoku ................. 113 5.4 Fast-Forward >> ....................... 116 6 Storing Local Data 117 6.1 Adding Options to Sudoku ................. 117 6.2 Continuing an Old Game .................. 119 6.3 Remembering the Current Position ............ 121 6.4 Accessing the Internal File System ............ 123 6.5 Accessing SD Cards ..................... 124 6.6 Fast-Forward >> ....................... 126 Report erratum Prepared exclusively for Antonio Pardo this copy is (P1.1 printing, May 26, 2008) CONTENTS 7 III Beyond the Basics 127 7 The Connected World 128 7.1 Browsing by Intent ..................... 129 7.2 Web with a View ....................... 132 7.3 From JavaScript to Java and Back ............ 137 7.4 Using Web Services ..................... 144 7.5 Fast-Forward >> ....................... 155 8 Locating and Sensing 156 8.1 Location, Location, Location ................ 156 8.2 Set Sensors to Maximum .................. 162 8.3 Bird’s-Eye View ....................... 165 8.4 Fast-Forward >> ....................... 171 9 Putting SQL to Work 173 9.1 Introducing SQLite ..................... 173 9.2 SQL 101 ........................... 174 9.3 Hello, Database ....................... 176 9.4 Data Binding ......................... 184 9.5 Using a ContentProvider .................. 186 9.6 Implementing a ContentProvider ............. 190 9.7 Fast-Forward >> ....................... 191 10 3D Graphics in OpenGL 193 10.1 Understanding 3D Graphics ................ 193 10.2 Introducing OpenGL .................... 194 10.3 Building an OpenGL Program ............... 195 10.4 Managing Threads ..................... 197 10.5 Building a Model ...................... 203 10.6 Lights, Camera, ... ...................... 206 10.7 Action! ............................ 208 10.8 Applying Texture ...................... 211 10.9 Peekaboo ........................... 214 10.10 Fast-Forward >> ....................... 215 Report erratum Prepared exclusively for Antonio Pardo this copy is (P1.1 printing, May 26, 2008) CONTENTS 8 IV Appendixes 216 A Java vs. the Android Language and APIs 217 A.1 Language Subset ...................... 217 A.2 Standard Library Subset .................. 219 A.3 Third-Party Libraries .................... 220 B Creating a Widget 221 C Publishing to the Android Market 222 D Bibliography 223 Index 224 Report erratum Prepared exclusively for Antonio Pardo this copy is (P1.1 printing, May 26, 2008) Acknowledgments I’d like to thank the many people who made this book possible, includ- ing my reviewers Anthony Stevens, Gabor Paller, Fred Burke, Dianne Hackborn, and Laurent Pontier for their attention to detail; my editor Susannah Pfalzer for her great suggestions and good cheer in the face of impossible deadlines; and especially my family for their patience in putting up with all the long hours. Prepared exclusively for Antonio Pardo Changes (Cupcake Updates) Android 1.5, also known as Cupcake, was released this spring. Cup- cake contains a number of user- and programmer-facing changes that broke examples in this book, and rendered some sections obsolete. This series of updates will address these changes, and add new sections and appendixes based on the Cupcake release. All changes since the original printing will be flagged with an orange color, like this text. P1.1—May 26 This update contains the following changes: • Chapter , Changes (Cupcake Updates): New section (you’re reading it now) that will detail the changes made in each release. • Chapter , Preface, on page 12: Updated for Cupcake. • Chapter 1, Quick Start, on page 18: Updated for Cupcake. Added new material on Android Virtual Devices (AVDs). Took new screen- shots with 1.5_r1 and ADT 0.9.1v200905011822-1621. • Appendix B, on page 221: New appendix on writing widgets (just a placeholder for now). • Appendix C, on page 222: New appendix on signing and publishing (just a placeholder for now). • Updated all sample source code files1 so they build and run with 1.5. I haven’t updated the text that describes the samples. • Tested all samples on 1.5 firmware on a real phone except for MyMap. 1. http://www.pragprog.com/titles/eband/source_code Prepared exclusively for Antonio Pardo P1.1—MAY 26 11 • Various: Cleared up all outstanding errata.2 • Various: Fixed URLs that Google broke since the first printing. TODO in future releases: • Review the remaining chapters and make changes as needed. • Double-check that sample code changes are reflected in the text that surrounds them. • Update the MyMap sample to use new zoom API. • Update