Tải bản đầy đủ (.pdf) (594 trang)

Android in Action SECOND EDITION ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (16.26 MB, 594 trang )

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:
©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>
CONTENTSx
6
Networking and web services 159
6.1 An overview of networking 161
Networking basics 161

Clients and servers 163
6.2 Checking the network status 164
6.3 Communicating with a server socket 165
6.4 Working with HTTP 168
Simple HTTP and java.net 169

Robust HTTP with
HttpClient 170

Creating an HTTP and HTTPS helper 172
6.5 Web services 178
POX—Putting it together with HTTP and XML 179
REST 181

To SOAP or not to SOAP, that is the question 184
6.6 Summary 185
7

Telephony 187
7.1 Exploring telephony background and terms 188
Understanding GSM 189

Understanding CDMA 189
7.2 Accessing telephony information 190
Retrieving telephony properties 191

Obtaining phone state
information 193
7.3 Interacting with the phone 195
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 messages 199

Receiving SMS messages 202
7.5 Summary 203
8
Notifications and alarms 205
8.1 Introducing Toast 206
Creating an SMS example with a Toast 206

Receiving an
SMS message 207
8.2 Introducing notifications 210

The Notification class 210

Notifying a user of an SMS 211
8.3 Introducing Alarms 215
Creating a simple alarm example 215

Using notifications
with Alarms 218
8.4 Summary 220
Download from Wow! eBook <www.wowebook.com>
CONTENTS xi
9
Graphics and animation 222
9.1 Drawing graphics in Android 223
Drawing with XML 224

Exploring XML drawable
shapes 225
9.2 Creating animations with Android’s Graphics API 227
Android’s frame-by-frame animation 227

Programmatically
creating an animation 230
9.3 Introducing OpenGL for Embedded Systems 233
Creating an OpenGL context 234

Drawing a rectangle with
OpenGL ES 238

Three-dimensional shapes and surfaces with

OpenGL ES 241
9.4 Summary 245
10
Multimedia 246
10.1 Introduction to multimedia and OpenCORE 247
10.2 Playing audio 248
10.3 Playing video 250
10.4 Capturing media 251
Understanding the camera 252

Capturing audio 257
10.5 Recording video 259
10.6 Summary 265
11
Location, location, location 267
11.1 Simulating your location within the emulator 269
Sending in your coordinates with the DDMS tool 269
The GPS Exchange Format 271

The Google Earth
Keyhole Markup Language 272
11.2 Using LocationManager and LocationProvider 275
Accessing location data with LocationManager 275
Using a LocationProvider 277

Receiving location
updates with LocationListener 279
11.3 Working with maps 281
Extending MapActivity 282


Using a MapView 282
Placing data on a map with an Overlay 285
11.4 Converting places and addresses with Geocoder 288
11.5 Summary 290
Download from Wow! eBook <www.wowebook.com>
CONTENTSxii
PART 3 ANDROID APPLICATIONS 291
12
Putting Android to work in a field service application 293
12.1 Designing a real-world Android application 294
Core requirements of the application 295

Managing the
data 296

Application architecture and integration 297
12.2 Mapping out the application flow 298
Mapping out the field service application 298

List of source
files 300

Field service application’s AndroidManifest.xml 302
12.3 Application source code 302
Splash Activity 302

Preferences used by the FieldService
Activity 304

Implementing the FieldService Activity 306

Settings 307

Managing job data 309
12.4 Source code for managing jobs 316
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
12.5 Server code 333
Dispatcher user interface 334

Database 334

PHP
dispatcher code 335

PHP mobile integration code 336
12.6 Summary 337
13
Building Android applications in C 338
13.1 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
application 346

exit() versus return() 349

Startup
code 350
13.3 What time is it? The DayTime Server 352
DayTime Server application 352

daytime.c 353
The SQLite database 355

Building and running the
DayTime Server 358
13.4 Daytime Client 360
Activity 360

Socket Client 361

Testing the Daytime
Client 362
13.5 Summary 362
Download from Wow! eBook <www.wowebook.com>
CONTENTS xiii
PART 4 THE MATURING PLATFORM . 365

14
Bluetooth and sensors 367
14.1 Exploring Android’s Bluetooth capabilities 368
Replacing cables 369

Primary and secondary roles and
sockets 369

Trusting a device 370

Connecting to a remote
device 372

Capturing Bluetooth events 374

Bluetooth
permissions 375
14.2 Interacting with the SensorManager 375
Types of sensors 376

Reading sensor values 377
Enabling and disabling sensors 378
14.3 Building the SenseBot application 379
User interface 380

Interpreting sensor values 382
Driving the robot 383

Communication with the robot 384
14.4 Summary 385

15
Integration 387
15.1 Understanding the Android contact model 388
Choosing open-ended records 388

Dealing with multiple
accounts 390

Unifying a local view from diverse remote
stores 392

Sharing the playground 393
15.2 Getting started with LinkedIn 393
15.3 Managing contacts 395
Leveraging the built-in contacts app 395

Requesting operations
from your app 398

Directly reading and modifying the contacts
database 399

Adding contacts 400
15.4 Keeping it together 403
The dream of sync 403

Defining accounts 404
Telling secrets: The AccountManager service 405
15.5 Creating a LinkedIn account 406
Not friendly to mobile 406


Authenticating to LinkedIn 407
15.6 Synchronizing to the backend with SyncAdapter 414
The synchronizing lifecycle 414

Synchronizing LinkedIn
data 414
15.7 Wrapping up: LinkedIn in action 417
Finalizing the LinkedIn project 417

Troubleshooting tips 418
Moving on 419
15.8 Summary 419
Download from Wow! eBook <www.wowebook.com>
CONTENTSxiv
16
Android web development 421
16.1 What’s Android web development? 422
Introducing WebKit 422

Examining the architectural
options 423
16.2 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 made-
for-mobile application 430
16.3 Storing data 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
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
16.5 Summary 453
17
AppWidgets 454
17.1 Introducing the AppWidget 455
What’s an AppWidget? 455

AppWidget deployment
strategies 457
17.2 Introducing SiteMonitor 458
Benefits of SiteMonitor 458

The user experience 459
17.3 SiteMonitor application architecture 462
Bird’s-eye view of the application 462

File by file 464
17.4 AppWidget data handling 465
17.5 Implementing the AppWidgetProvider 469
AppWidgetProvider method inventory 469

Implementing

SiteMonitorWidgetImpl 470

Handling zombie widgets 472
17.6 Displaying an AppWidget with RemoteViews 473
Working with RemoteViews 473

UpdateOneWidget
explained 474
Download from Wow! eBook <www.wowebook.com>
CONTENTS xv
17.7 Configuring an instance of the AppWidget 476
AppWidget metadata 477

Working with Intent data 478
Confirming widget creation 479
17.8 Updating the AppWidget 480
Comparing services to alarms 481

Triggering the update 482
Updating the widgets, finally! 484
17.9 Tying it all together with AndroidManifest.xml 488
17.10 Summary 489
18
Localization 491
18.1 The need for localization 492
18.2 Exploring locales 493
18.3 Strategies for localizing an application 494
Identifying target locales and data 494

Identifying and

managing strings 495

Drawables and layouts 497
Dates, times, numbers, and currencies 498

Working with
the translation team 499
18.4 Leveraging Android resource capabilities 500
More than locale 500

Assigning strings in resources 500
18.5 Localizing in Java code 502
18.6 Formatting localized strings 503
18.7 Obstacles to localization 504
18.8 Summary 505
19
Android Native Development Kit 506
19.1 Introducing the NDK 507
Uses for the NDK 507

Looking at the NDK 508
19.2 Building an application with the NDK 509
Demonstrating the completed application 510
Examining the project structure 511
19.3 Building the JNI library 512
Understanding JNI 512

Implementing the library 513
Compiling the JNI library 518
19.4 Building the user interface 519

User interface layout 519

Taking a photo 521
Finding the edges 523
Download from Wow! eBook <www.wowebook.com>
CONTENTSxvi
19.5 Integrating the NDK into Eclipse 524
19.6 Summary 526
appendix A Installing the Android SDK 527
appendix B Publishing applications 538
index 551
Download from Wow! eBook <www.wowebook.com>
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.
Download from Wow! eBook <www.wowebook.com>
PREFACExviii
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!
FRANK ABLESON
Download from Wow! eBook <www.wowebook.com>
xix
preface to the first edition
The first mobile applications I had the opportunity to work with were inventory con-
trol programs used in retail and manufacturing settings. The “terminals,” as we called
them at the time, were heavy and expensive. They had big antennas, lots of clunky
keys, grayscale LCD displays, and they looked like they came straight from the set of a
science fiction movie.

From that austere beginning, my mobile horizons expanded when the Palm Pilot
became the craze in the mid to late 1990s. My first significant PalmOS project was to
develop an IrDA communications library for an application that printed calendars,
contacts, and task-lists. Back then, the hip printers had an IrDA port and it was cool to
beam your business card to someone. Ironically, I always enjoyed designing and writ-
ing the software more than using the devices themselves.
Fast forward ten years, and I have had the privilege of working on some very chal-
lenging and engaging mobile software projects for numerous clients along the way.
Much of my career to date can be traced back to relationships stemming from my
early mobile development experiences—and what a blessing it has been for me. I just
love the question, “would it be possible to…?” And more often than not, the answer
has been “Yes!” What I particularly enjoy is helping change the way a business operates
or the way problems are solved through the application of mobile software. Mobile
technology can and will continue to change the way we live, work, and play…and this
brings me to Android and this book.
In the fall of 2007, I was speaking with my friend Troy Mott, who happens to be an
editor for Manning, the publisher of this book. Troy and I were discussing the mobile
marketplace, something we’ve been doing for years. We started kicking around the
Download from Wow! eBook <www.wowebook.com>
PREFACE TO THE FIRST EDITIONxx
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.
FRANK ABLESON
Download from Wow! eBook <www.wowebook.com>
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
Download from Wow! eBook <www.wowebook.com>
ACKNOWLEDGMENTSxxii
product when looking at edits upon edits in MS Word, but Dottie’s magic makes the
product you hold in your hands. Thanks to each of you for your special contribution
to this project. Next, we would like to thank Candace Gillhooley for her efforts in get-
ting the word out about the book.
And special thanks to the other reviewers who read our revised manuscript at dif-
ferent times during its development: Michael Martin, Orhan Alkan, Eric Raymond,
Jason Jung, Frank Wang, Robert O’Connor, Paul Grebenc, Sean Owen, Loïc Simon,
Greg Donald, Nikolaos Kaintantzis, Matthew Johnson, and Patrick Steger; and to
Michael Galpin and Jérôme Bâton for their careful tech review of the final manuscript
during production.
Lastly, we want to thank the thoughtful and encouraging MEAP subscribers who
provided feedback along the way; the book is better thanks to your contributions.
Frank Ableson
I would like to thank Robi Sen, Chris King, and Troy Mott for their contributions, col-
laboration, and endurance on this project! And of course, my wife Nikki and my chil-

dren deserve special recognition for the seemingly endless hours of wondering when I
would emerge from the “lab” and what mood I would be in—either elation when the
robot worked, or near depression when the AppWidgets wouldn’t go away. Thank you
for getting neither too excited nor too concerned! My staff at navitend also deserve a
big thank you for carrying the water while I finished my work on this project. Finally, a
big thank you to Miriam Raffay from Madridiam.com, who provided the much-
needed Spanish translations for chapter 18. Gracias!
Chris King
I am deeply grateful to Troy Mott and Frank Ableson for bringing me into this project
and providing support and inspiration throughout. Troy has been welcoming and
enthusiastic, showing great flexibility as we discussed what projects to undertake.
Frank has a keen eye for quality, and provided great guidance from start to finish on
how to craft the best book possible. I also appreciate all the work done by the review-
ers and editors from Manning, whose contributions have improved the text’s accuracy
and style. Working on this book has been a joy, and I’ve greatly enjoyed the opportuni-
ties to contribute more and more to its progress.
Thanks also to the crew at Gravity Mobile, especially Noah Hurwitz, Chris Lyon,
Young Yoon, and Sam Trychin. You guys keep my life fun and challenging, and have
made mobile development an even better place to work. Finally, my love to my fam-
ily: Charles, Karen, Patrick, Kathryn, and Andrew. You’ve made everything possible
for me.
Download from Wow! eBook <www.wowebook.com>
ACKNOWLEDGMENTS xxiii
Robi Sen
I would like to thank Troy Mott and the team—and everyone at Manning Publica-
tions— for their hard work making this book something worth reading. I would like to
thank my coauthors, Frank and Chris, who were great to work with and very under-
standing when I was the one holding things up. I would also like to thank Jesse Dailey
for his help with OpenGL as well as David Cartier with the Contacts API. Finally, I
would like to thank my family who, more often than I liked, had to do without me

while I worked on my chapters, worked multiple jobs, and finished grad school.
Download from Wow! eBook <www.wowebook.com>
xxiv
about this book
Android in Action, Second Edition is a revision and update of Unlocking Android, pub-
lished in April 2009. This book doesn’t fit nicely into the camp of “introductory text,”
nor is it a highly detailed reference manual. The text has something to offer both the
beginner and the experienced developer who is looking to sell his or her application
in the Android Market. This book covers important beginner topics such as “What is
Android” and installing and using the development environment. We then advance to
practical working examples of core programming topics any developer will be happy
to have at the ready on the reference shelf. The remaining chapters present very
detailed example applications covering advanced topics, including a complete field
service application, localization, and material on Android web applications, Blue-
tooth, sensors, AppWidgets, and integration adapters. We even include two chapters
on writing applications in C—one for the native side of Android and one using the
more generally accepted method of employing the Android Native Development Kit.
Although you can read the book from start to finish, you can also consider it a cou-
ple of books in one. If you’re new to Android, focus first on chapter 1, appendix A,
and then chapter 2. With that foundation, you can then work your way through chap-
ters 3 through 12. Chapter 13 and on are more in-depth in nature and can be read
independently of the others.
The audience
We wrote this book for professional programmers and hobbyists alike. Many of the
concepts can be absorbed without specific Java language knowledge, though the
most value will be found by readers with Java programming skills because Android
Download from Wow! eBook <www.wowebook.com>

×