MANNING W. Frank Ableson Robi Sen Chris King Covers Android 2 SECOND EDITION IN ACTION Android in Action Second Edition Download from Wow! eBook <www.wowebook.com> Download from Wow! eBook <www.wowebook.com> Android in Action SECOND EDITION W. FRANK ABLESON ROBI SEN CHRIS KING Revised Edition of Unlocking Android MANNING Greenwich (74° w. long.) Download from Wow! eBook <www.wowebook.com> 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. 180 Broad St. Suite 1323 Stamford, CT 06901 Email: orders@manning.com ©2011 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: Troy Mott 180 Broad St. Copyeditors: Joan Celmer, Liz Welch Suite 1323 Typesetter: Dottie Marsico Stamford, CT 06901 Cover designer: Marija Tudor ISBN 978-1-935182-72-6 Printed in the United States of America 12 34567 8910– MAL–16 151413 1211 Download from Wow! eBook <www.wowebook.com> v brief contents PART 1 WHAT IS ANDROID?—THE BIG PICTURE 1 1 ■ Introducing Android 3 2 ■ Android’s development environment 31 PART 2 EXERCISING THE ANDROID SDK 61 3 ■ User interfaces 63 4 ■ Intents and Services 101 5 ■ Storing and retrieving data 129 6 ■ Networking and web services 159 7 ■ Telephony 187 8 ■ Notifications and alarms 205 9 ■ Graphics and animation 222 10 ■ Multimedia 246 11 ■ Location, location, location 267 PART 3 ANDROID APPLICATIONS 291 12 ■ Putting Android to work in a field service application 293 13 ■ Building Android applications in C 338 Download from Wow! eBook <www.wowebook.com> BRIEF CONTENTSvi PART 4 THE MATURING PLATFORM 365 14 ■ Bluetooth and sensors 367 15 ■ Integration 387 16 ■ Android web development 421 17 ■ AppWidgets 454 18 ■ Localization 491 19 ■ Android Native Development Kit 506 Download from Wow! eBook <www.wowebook.com> vii contents preface xvii preface to the first edition xix acknowledgments xxi about this book xxiv about the cover illustration xxix PART 1 WHAT IS ANDROID?—THE BIG PICTURE 1 1 Introducing Android 3 1.1 The Android platform 4 1.2 Understanding the Android market 5 Mobile operators 5 ■ Android vs. the feature phones 6 Android vs. the smartphones 7 ■ Android vs. itself 8 Licensing Android 9 1.3 The layers of Android 10 Building on the Linux kernel 11 ■ Running in the Dalvik VM 12 1.4 The Intent of Android development 12 Empowering intuitive UIs 13 ■ Intents and how they work 13 Download from Wow! eBook <www.wowebook.com> CONTENTSviii 1.5 Four kinds of Android components 17 Activity 17 ■ Service 18 ■ BroadcastReceiver 19 ContentProvider 22 1.6 Understanding the AndroidManifest.xml file 24 1.7 Mapping applications to processes 25 1.8 Creating an Android application 26 1.9 Summary 30 2 Android’s development environment 31 2.1 Introducing the Android SDK 32 Core Android packages 33 ■ Optional packages 34 2.2 Exploring the development environment 34 The Java perspective 35 ■ The DDMS perspective 37 Command-line tools 40 2.3 Building an Android application in Eclipse 43 The Android Project Wizard 43 ■ Android sample application code 44 ■ Packaging the application 50 2.4 Using the Android emulator 51 Setting up the emulated environment 52 Testing your application in the emulator 56 2.5 Debugging your application 57 2.6 Summary 58 PART 2 EXERCISING THE ANDROID SDK 61 3 User interfaces 63 3.1 Creating the Activity 65 Creating an Activity class 66 ■ Exploring the Activity lifecycle 71 3.2 Working with views 74 Exploring common views 75 ■ Using a ListView 77 Multitasking with Handler and Message 81 Creating custom views 82 ■ Understanding layout 84 Handling focus 86 ■ Grasping events 87 3.3 Using resources 89 Supported resource types 89 ■ Referencing resources in Java 89 Defining views and layouts through XML resources 92 Externalizing values 94 ■ Providing animations 97 Download from Wow! eBook <www.wowebook.com> CONTENTS ix 3.4 Exploring the AndroidManifest file 98 3.5 Summary 99 4 Intents and Services 101 4.1 Serving up RestaurantFinder with Intent 102 Defining Intents 102 ■ Implicit and explicit invocation 103 Adding external links to RestaurantFinder 104 ■ Finding your way with Intent 106 ■ Taking advantage of Android-provided activities 108 4.2 Checking the weather with a custom URI 109 Offering a custom URI 109 ■ Inspecting a custom Uri 111 4.3 Checking the weather with broadcast receivers 113 Broadcasting Intent 113 ■ Creating a receiver 115 4.4 Building a background weather service 115 4.5 Communicating with the WeatherAlertService from other apps 119 Android Interface Definition Language 119 ■ Binder and Parcelable 121 ■ Exposing a remote interface 122 Binding to a Service 123 ■ Starting versus binding 126 Service lifecycle 127 4.6 Summary 128 5 Storing and retrieving data 129 5.1 Using preferences 130 Working with SharedPreferences 130 ■ Preference access permissions 133 5.2 Using the filesystem 136 Creating files 136 ■ Accessing files 137 ■ Files as raw resources 138 ■ XML file resources 139 ■ External storage via an SD card 141 5.3 Persisting data to a database 144 Building and accessing a database 144 ■ Using the sqlite3 tool 149 5.4 Working with ContentProvider classes 149 Using an existing ContentProvider 150 ■ Creating a ContentProvider 151 5.5 Summary 158 Download from Wow! eBook <www.wowebook.com> [...]... AccountManager service 405 ■ 15.5 Creating a LinkedIn account Not friendly to mobile 15.6 406 414 ■ Summary 407 414 Synchronizing LinkedIn Wrapping up: LinkedIn in action Finalizing the LinkedIn project Moving on 419 15.8 406 Authenticating to LinkedIn Synchronizing to the backend with SyncAdapter The synchronizing lifecycle data 414 15.7 ■ 417 417 ■ Troubleshooting tips 419 Download from Wow! eBook... content originally written by Charlie Collins remains in this second edition Early on in the project Chris and I were discussing the need to bring social networking into the book Chris examined the available social networks and came back with a clever mechanism to integrate the Android contacts database with the popular business networking service LinkedIn His work is shown in chapter 15, “Integration.”... directly in the browser 431 Setting things up 432 Examining the code 433 The user interface 433 Opening the database 435 Unpacking the transaction function 436 Inserting and deleting rows 438 Testing the application with WebKit tools 439 ■ ■ ■ ■ ■ 16.4 Building a hybrid application 440 Examining the browser control 440 Wiring up the control 441 Implementing the JavaScript handler 443 Accessing the code... 187 Exploring telephony background and terms Understanding GSM 7.2 189 ■ Understanding CDMA Accessing telephony information Retrieving telephony properties information 193 7.3 188 191 Interacting with the phone 189 190 195 ■ Obtaining phone state Using intents to make calls 195 Using phone number-related utilities 196 Intercepting outbound calls 198 ■ ■ 7.4 Working with messaging: SMS 199 Sending SMS... the Android Developer Tools plug -in for Eclipse A note about the graphics Many of the original graphics from the first edition, Unlocking Android, have been reused in this version of the book While the title of the revised edition was changed to Android in Action, Second Edition during development, we kept the original book title in our graphics and sample applications Author Online Purchase of Android. .. Examining the project structure 511 19.3 500 Localizing in Java code 502 Formatting localized strings 503 Obstacles to localization 504 Summary 505 Android Native Development Kit 19.1 500 Building the JNI library 508 509 510 512 Understanding JNI 512 Implementing the library 513 Compiling the JNI library 518 ■ 19.4 Building the user interface User interface layout 519 Finding the edges 523 ■ 519 Taking... 16 Android web development 16.1 What’s Android web development? 422 Introducing WebKit options 423 16.2 421 422 Examining the architectural ■ Optimizing web applications for Android 424 Designing with mobile in mind 424 Adding the viewport tag 426 Selectively loading content 428 Interrogating the user agent 428 The media query 429 Considering a madefor-mobile application 430 ■ ■ ■ ■ 16.3 ■ Storing... mobile integration code 336 ■ ■ ■ 12.6 13 Summary 337 Building Android applications in C 13.1 338 Building Android apps without the SDK 339 The C compiler and linker tools 339 Building a Hello World application 340 Installing and running the application 342 C application build script 344 ■ ■ 13.2 Solving the problem with dynamic linking 344 Android system libraries 345 Building a dynamically linked... xxvii Chapter 15 explores the Android contact database and demonstrates integrating with an external data source In particular, this application brings Android into the social networking scene by integrating with the popular LinkedIn professional networking service Chapter 16 explores the world of web development Android s browser is based on the open source WebKit engine and brings desktop-like capability... very long lines will include line-continuation markers Source code for all the working examples is available from www.manning.com/ AndroidinActionSecondEdition or http://www.manning.com/ableson2 A readme.txt file is provided in the root folder and also in each chapter folder; the files provide details on how to install and run the code Code examples appear throughout this book Longer listings will appear . synchronizing lifecycle 414 ■ Synchronizing LinkedIn data 414 15.7 Wrapping up: LinkedIn in action 417 Finalizing the LinkedIn project 417 ■ Troubleshooting. MANNING W. Frank Ableson Robi Sen Chris King Covers Android 2 SECOND EDITION IN ACTION Android in Action Second Edition Download from