




























































































Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Esta página não é visível na pré-visualização
Não perca as partes importantes!





























































































Revised Edition of Unlocking Android
CONTENTS ix
4
Intents and Services 101
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
Offering a custom URI 109 ■^ Inspecting a custom Uri 111
Broadcasting Intent 113 ■^ Creating a receiver 115
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
5
Storing and retrieving data 129
Working with SharedPreferences 130 ■^ Preference access permissions 133
Creating files 136 ■^ Accessing files 137 ■^ Files as raw resources 138 ■^ XML file resources 139 ■^ External storage via an SD card 141
Building and accessing a database 144 ■^ Using the sqlite3 tool 149
Using an existing ContentProvider 150 ■^ Creating a ContentProvider 151
CONTENTS xi
9
Graphics and animation 222
Drawing with XML 224 ■^ Exploring XML drawable shapes 225
Android’s frame-by-frame animation 227 ■^ Programmatically creating an animation 230
Creating an OpenGL context 234 ■^ Drawing a rectangle with OpenGL ES 238 ■^ Three-dimensional shapes and surfaces with OpenGL ES 241
10
Multimedia 246
Understanding the camera 252 ■^ Capturing audio 257
11
Location, location, location 267
Sending in your coordinates with the DDMS tool 269 The GPS Exchange Format 271 ■^ The Google Earth Keyhole Markup Language 272
Accessing location data with LocationManager 275 Using a LocationProvider 277 ■^ Receiving location updates with LocationListener 279
Extending MapActivity 282 ■^ Using a MapView 282 Placing data on a map with an Overlay 285
xii CONTENTS
P ART 3 A NDROID APPLICATIONS ...................................
12
Putting Android to work in a field service application 293
Core requirements of the application 295 ■^ Managing the data 296 ■^ Application architecture and integration 297
Mapping out the field service application 298 ■^ List of source files 300 ■^ Field service application’s AndroidManifest.xml 302
Splash Activity 302 ■^ Preferences used by the FieldService Activity 304 ■^ Implementing the FieldService Activity 306 Settings 307 ■^ Managing job data 309
RefreshJobs 317 ■^ Managing jobs: The ManageJobs Activity 320 ■^ Working with a job with the ShowJob Activity 323 Capturing a signature with the CloseJob Activity 327
Dispatcher user interface 334 ■^ Database 334 ■^ PHP dispatcher code 335 ■^ PHP mobile integration code 336
13
Building Android applications in C 338
The C compiler and linker tools 339 ■^ Building a Hello World application 340 ■^ Installing and running the application 342 C application build script 344
Android system libraries 345 ■^ Building a dynamically linked application 346 ■^ exit() versus return() 349 ■^ Startup code 350
DayTime Server application 352 ■^ daytime.c 353 The SQLite database 355 ■^ Building and running the DayTime Server 358
Activity 360 ■^ Socket Client 361 ■^ Testing the Daytime Client 362
xiv CONTENTS
16
Android web development 421
Introducing WebKit 422 ■^ Examining the architectural options 423
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 made- for-mobile application 430
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
Examining the browser control 440 ■^ Wiring up the control 441 Implementing the JavaScript handler 443 ■^ Accessing the code from JavaScript 445 ■^ Digging into the JavaScript 445 Security matters 447 ■^ Implementing a WebViewClient 448 Augmenting the browser 448 ■^ Detecting navigation events 449 Implementing the WebChromeClient 452
17
AppWidgets 454
What’s an AppWidget? 455 ■^ AppWidget deployment strategies 457
Benefits of SiteMonitor 458 ■^ The user experience 459
Bird’s-eye view of the application 462 ■^ File by file 464
AppWidgetProvider method inventory 469 ■^ Implementing SiteMonitorWidgetImpl 470 ■^ Handling zombie widgets 472
Working with RemoteViews 473 ■^ UpdateOneWidget explained 474
xvii
preface
When we set out to write the first version of this book, many friends and family won- dered just what this Android thing was all about. Now, two years after the publication of the first edition, Android is nearly a household term. The first edition of the book, Unlocking Android, enjoyed enough success that we were privileged to have the opportunity to write this second edition, renamed as Android in Action. The first thirteen chapters of the book have been refreshed and/or rewritten to bring the content up to date with Android 2.2+. Six chapters were added, bringing in more topics of interest that stray from the simplistic but are still within the realm of instructional and informational. The new content extends beyond the basics of Android development, including some topics that I’ve envisioned for a long time but lacked the proper platform to bring them to fruition. We could have written many more chapters, but we had to draw the line somewhere! The second edition of this book was written by Frank Ableson, Robi Sen, and Chris King. Chris updated chapters 4, 5, 7, and 11. Some excellent content originally writ- ten 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 exam- ined 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.” The application from chap- ter 15 is available as a free download in the Android Market. Robi updated his chapters on notifications, graphics, and media, while I focused on some new content areas of interest, including Bluetooth communications, sen- sors, localization, AppWidgets, native development in C, and web development for Android.
xviii PREFACE
In addition to the LinkedIn application from chapter 15, two more applications from this book are available in the Market as free downloads. The first is SenseBot— an application that allows you to drive a LEGO Mindstorms-powered robot by tilting your phone. The application demonstrates both the sensor subsystem of Android, as well as communicating with Bluetooth. The other application available in the Market is called FindEdges. FindEdges demonstrates the Android Native Development Kit as it exercises an image processing algorithm written in the C language. All in all, writing a book for Android is both exciting and challenging. Android continues to mature and promises to be a major player for years to come. Many thanks are owed to readers of the first edition, for without you, there wouldn’t be a second edition! F RANK ABLESON
xx PREFACE TO THE FIRST EDITION
idea of writing a book on Android. The challenge was that Android didn’t really exist. Yet. We knew from some of the preliminary information that the platform promised to be open, capable, and popular. We felt that those ingredients could make for an inter- esting and valuable topic, so we began thinking about what that book might look like, taking it on faith that the platform would actually come to fruition. Before long, we convinced ourselves (and Manning) that this was a good idea and the work began in early 2008. Beyond the usual challenges of putting a book together, we had the additional obstacle that our subject matter has been in a steady, though unpredictable, state of change over the past year. In essence, we’ve written this book twice because the SDK has been changed multiple times and Android-equipped phones have become available, accelerating the interest and demand for the plat- form. Every time a significant change occurred, we went back and revisited portions of the book, sometimes rewriting entire chapters to accommodate the latest develop- ments in the Android platform. I say “we” because in the process of writing this book, Troy and I decided to share the fun and brought in two experienced authors to contribute their expertise and enthusiasm for this platform. It has been a pleasure getting to know and working with both Charlie Collins and Robi Sen. While I focused on the first and third parts of the book in the first edition, Charlie and Robi wrote part 2, which covers the important fundamentals of writing Android applications. Thanks to their contributions, I enjoyed the freedom to express my vision of what Android means to the mobile space in the first part of the book, and then to work on a couple of more advanced applica- tions at the end of the book. We hope that you enjoy reading this book and that it proves to be a valuable resource for years to come as together we contribute to the future of the Android platform. F RANK ABLESON
xxi
acknowledgments
Perhaps the only thing more challenging than writing a technical book is writing the second edition. There is a lot of excitement when writing the proposed table of con- tents for the updated edition but at some point the work must commence. The size and scope of this project meant working together as a team from the start. I had the privi- lege of working again with Robi Sen from the first edition and also with experienced developer and writer Chris King. Along with the help of the talented team at Manning, we are pleased to present Android in Action, the update to Unlocking Android. In particular, we’d like to acknowledge and thank those at Manning who helped bring this book about. First, thanks to Troy Mott, our acquisition and development editor, who has been involved in every aspect of both the first and second editions. Troy was there from the beginning, from the “what if” stages, through helping push us over the goal line—twice! Karen Tegtmeyer did all the big and little things to bring the project together; Mary Piergies skillfully piloted the team through the harrowing production process; and Marjan Bace, our publisher, showed an attention to detail at once challenging, beneficial, and appreciated. Once the writing was done, the next round of work began and special thanks need to go to: Benjamin Berg who performed the pre-production editing pass, Joan Celmer and Liz Welch, our copyeditors, who made our content readable in cases where it went either “too geek” or where the geek in us tried to be “too literary;” Elizabeth Martin, our proofreader, who added common sense to the project, as well as a terrific sense of humor and encouraging attitude; Janet Vail who jumped in at the last minute to help us bring the final pieces of the project together; and finally Dottie Marsico who handles the actual layout of the pages. It is sometimes hard to envision the final