Charlie Collins Michael Galpin Matthias Käppler P IN RACTICE Includes 91 Techniques M A N N I N G Android in Practice Android in Practice CHARLIE COLLINS MICHAEL D. GALPIN MATTHIAS KÄPPLER MANNING SHELTER ISLAND For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email: [email protected] ©2012 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co.Development editor:Cynthia Kane 20 Baldwin Road Copyeditor: Benjamin Berg PO Box 261 Typesetter: Gordan Salinovic Shelter Island, NY 11964 Cover designer: Marija Tudor ISBN 9781935182924 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 16 15 14 13 12 11 brief contents P 1 B ......................................1 ART ACKGROUND AND FUNDAMENTALS 1 ■ Introducing Android 3 2 ■ Android application fundamentals 40 3 ■ Managing lifecycle and state 73 P 2 R ......................................................99 ART EAL WORLD RECIPES 4 ■ Getting the pixels perfect 101 5 ■ Managing background tasks with Services 155 6 ■ Threads and concurrency 189 7 ■ Storing data locally 224 8 ■ Sharing data between apps 266 9 ■ HTTP networking and web services 295 10 ■ Location is everything 334 11 ■ Appeal to the senses using multimedia 363 12 ■ 2D and 3D drawing 402 P 3 B ...................................441 ART EYOND STANDARD DEVELOPMENT 13 ■ Testing and instrumentation 443 14 ■ Build management 489 15 ■ Developing for Android tablets 540 v contents preface xiii acknowledgments xv about this book xviii about the cover illustration xxii P 1 B ............................1 ART ACKGROUND AND FUNDAMENTALS 1 Introducing Android 3 1.1 Android in a nutshell 5 1.2 Hello Android! 9 1.3 Java, but not Java Java 19 1.4 Linux, but not Linux Linux 24 1.5 More capabilities with native libraries 29 1.6 Tools of the trade 32 1.7 Summary 38 2 Android application fundamentals 40 2.1 The DealDroid application 41 2.2 Core building blocks 43 vii viii CONTENTS 2.3 Application manifest 44 2.4 Resources 46 2.5 Layout, views, and widgets 49 2.6 Activities 51 2.7 Adapters 59 2.8 Intents and IntentFilters 63 2.9 The Application object 69 2.10 Summary 71 3 Managing lifecycle and state 73 3.1 Defining an Android application 74 3.2 Knowing the Activity lifecycle 79 3.3 Controlling Activity instance state 90 3.4 Getting things done within a task 95 3.5 Summary 97 P 2 R ............................................99 ART EAL WORLD RECIPES 4 Getting the pixels perfect 101 4.1 The MyMovies application 102 4.2 View hierarchies and rendering 103 4.3 Arranging views in layouts 106 TECHNIQUE 1 The merge and include directives 114 4.4 Expanding on ListView and Adapter 117 TECHNIQUE 2 Managing a stateful list 118 TECHNIQUE 3 Header and footer views 122 4.5 Applying themes and styles 125 TECHNIQUE 4 Applying and writing styles 125 TECHNIQUE 5 Applying and writing themes 127 TECHNIQUE 6 Styling ListView backgrounds 129 4.6 Working with drawables 133 TECHNIQUE 7 Working with shape drawables 134 TECHNIQUE 8 Working with selector drawables 138 TECHNIQUE 9 Scaling views with nine-patch drawables 141 4.7 Creating portable user interfaces 144 TECHNIQUE 10 Automatically scaling to different screens 144 CONTENTS ix TECHNIQUE 11 Loading configuration dependent resources 149 TECHNIQUE 12 Programming pixel-independently 152 4.8 Summary 154 5 Managing background tasks with Services 155 5.1 It’s all about the multitasking 156 5.2 Why services and how to use them 157 TECHNIQUE 13 Creating a Service 158 TECHNIQUE 14 Starting a Service automatically 161 TECHNIQUE 15 Communicating with a Service 163 TECHNIQUE 16 Using a Service for caching data 169 TECHNIQUE 17 Creating notifications 171 5.3 Scheduling and Services 176 TECHNIQUE 18 Using the AlarmManager 177 TECHNIQUE 19 Keeping Services awake 180 TECHNIQUE 20 Using Cloud to Device Messaging 183 5.4 Summary 187 6 Threads and concurrency 189 6.1 Concurrency in Android 190 TECHNIQUE 21 Basic threading 191 TECHNIQUE 22 Communicating change between threads 195 TECHNIQUE 23 Managing threads in thread pools 200 6.2 Working with AsyncTask 205 TECHNIQUE 24 Implementing jobs with AsyncTask 206 TECHNIQUE 25 Preparing for configuration changes 210 6.3 Miscellaneous techniques 216 TECHNIQUE 26 Displaying splash screens with timers 216 TECHNIQUE 27 Implementing custom message loops 219 6.4 Summary 223 7 Storing data locally 224 7.1 Reading and writing files 225 TECHNIQUE 28 Using internal storage 226 TECHNIQUE 29 Using external storage 230 TECHNIQUE 30 Using cache directories 235 TECHNIQUE 31 Making sure files are saved with sync 236 7.2 Maintaining preferences 237 TECHNIQUE 32 Reading and writing preference data 237 TECHNIQUE 33 Using a PreferenceActivity 238
Description: