Contents at a Glance
Introduction ... 1
Book I: Android Jump-Start ... 9
Chapter 1: All about Android ... 11
Chapter 2: Installing the Software Tools ... 21
Chapter 3: Creating an Android App ... 43
Chapter 4: Conjuring and Embellishing an Android App ... 73
Book II: Android Background Material ... 109
Chapter 1: Using the Eclipse Workbench ... 111
Chapter 2: It’s Java! ... 145
Chapter 3: What Java Does (and When) ... 173
Chapter 4: Object-Oriented Programming in Java ... 191
Chapter 5: A <brief> Look at XML ... 207
Book III: The Building Blocks ... 223
Chapter 1: Android Activities ... 225
Chapter 2: Intents and Intent Filters... 259
Chapter 3: Services ... 297
Chapter 4: Broadcast Receivers ... 331
Chapter 5: Content Providers... 357
Book IV: Programming Cool Phone Features ... 381
Chapter 1: Lay Out Your Stuff ... 383
Chapter 2: Menus, Lists, and Notifi cations ... 407
Chapter 3: An Android Potpourri ... 433
Chapter 4: Apps for Tablets ... 465
Book V: The Job Isn’t Done Until . . . ... 483
Chapter 1: Publishing Your App to the Android Market ... 485
Chapter 2: Publishing Your App to the Amazon Appstore ... 515
COPYRIGHTED MATERIAL
Chapter 1: Creating Code Quickly with App Inventor ...527
Chapter 2: More App Inventor Magic ... 557
Chapter 3: How to “Rough It” without Eclipse ... 581
Chapter 4: Going Native ... 605
Index ... 625
Table of Contents
Introduction ... 1
How to Use This Book ... 1
Conventions Used in This Book ... 2
What You Don’t Have to Read ... 2
Foolish Assumptions ... 3
How This Book Is Organized ... 4
Book I: Android Jump-Start... 4
Book II: Android Background Material ... 4
Book III: The Building Blocks ... 4
Book IV: Programming Cool Phone Features ... 5
Book V: The Job Isn’t Done Until . . . ... 5
Book VI: Alternative Android Development Techniques ... 5
More on the Web! ... 6
Icons Used in This Book ... 6
Where to Go from Here ... 7
Book I: Android Jump-Start ... 9
Chapter 1: All about Android . . . .11
The Consumer Perspective ... 11
The Developer Perspective ... 15
Java ... 15
XML ... 16
Linux ... 17
The Business Perspective ... 19
Chapter 2: Installing the Software Tools . . . .21
Installing the Java Development Kit ... 21
Java for Windows, Linux, and Solaris ... 22
Java for Macintosh ... 25
Installing the Android SDK Starter Package ... 27
Installing the Eclipse Development Environment ... 29
Downloading Eclipse ... 29
Installing Eclipse ... 30
Confi guring Eclipse ... 31
Do I have the Eclipse Android Development Kit? ... 32
Dude, where’s my Android SDK? ... 35
Fattening Up the Android SDK ... 36
Installing platform tools ... 36
Creating an Android Virtual Device ... 38
Chapter 3: Creating an Android App . . . .43
Creating Your First App ... 43
Starting Eclipse ... 44
Creating a project ... 45
Running your project ... 49
What if . . . ... 53
Testing Apps on a Real Device ... 60
Examining a Basic Android App ... 62
A project’s fi les ... 63
The src directory ... 63
The gen directory ... 65
The res directory ... 67
The assets directory ... 68
Other fi les in an Android project ... 69
The android.jar archive ... 71
The bin directory ... 72
Chapter 4: Conjuring and Embellishing an Android App. . . .73
Dragging, Dropping, and Otherwise Tweaking an App ... 73
Creating the “look” ... 74
Coding the behavior ... 77
A Bit of Debugging ... 81
Try it! ... 82
More than one way to skin a LogCat ... 85
Improving Your App ... 87
Improving the layout ... 88
Creating a reusable layout ... 88
Starting another activity ... 91
Localizing your app ... 96
Responding to check box events ... 100
Displaying images ... 102
Sending in your order ... 106
Book II: Android Background Material ... 109
Chapter 1: Using the Eclipse Workbench . . . .111
What’s All That Stuff on the Eclipse Workbench? ... 112
Views and editors ... 114
Understanding the big picture ... 116
Table of Contents xiii
Juggling among perspectives ... 118
Changing the way a perspective looks ... 119
Where’s my view? ... 119
Some Useful Views ... 121
Views that normally live in the Java perspective ... 121
Views that normally live in the DDMS perspective ... 124
Be Nice to Your Java Code ... 127
Making it pretty ... 127
Let Eclipse do the typing ... 129
Generating getter and setter methods ... 130
Renaming things... 132
Creating Android strings ... 132
Using other refactoring actions ... 132
The Organize Imports action ... 133
Oops! ... 134
Some Things You Can Do with Eclipse Projects ... 135
Importing code ... 135
Creating a run confi guration ... 139
Chapter 2: It’s Java! . . . .145
From Development to Execution with Java ... 145
What is a compiler? ... 146
What is a virtual machine? ... 149
Grasping Java Code ... 151
The Java class ... 152
Classes and objects ... 152
Java types ... 154
The Java method ... 156
Objects and their constructors ... 160
Classes grow on trees... 163
The Java package ... 165
A public class ... 166
Other public things ... 167
Defying your parent ... 168
Java annotations ... 169
Java comments ... 169
Chapter 3: What Java Does (and When) . . . .173
Making Decisions (Java if Statements) ... 173
Testing for equality... 177
Choosing among many alternatives (Java switch statements).... 178
Repeating Instructions Over and Over Again ... 181
Java while statements ... 181
Java for statements ... 183
Java do statements ... 184
Arrays in Java ... 185
Java’s enhanced for statements ... 186
Jumping Away from Trouble ... 188
Chapter 4: Object-Oriented Programming in Java . . . .191
Static Fields and Methods ... 191
Interfaces and Callbacks ... 194
Event handling and callbacks ... 197
An object remembers who created it ... 199
An easier way to handle an event ... 200
Classes That Must (And Must Not) Be Extended ... 201
Java’s fi nal classes ... 202
Java’s abstract classes ... 202
Inner Classes ... 204
Named inner classes ... 204
Anonymous inner classes ... 205
Chapter 5: A <brief> Look at XML . . . .207
XML Isn’t Ordinary Text ... 208
Of tags and elements ... 209
Other things you fi nd in an XML document ... 215
What’s in a Namespace? ... 217
The package attribute ... 220
The style attribute ... 221
Book III: The Building Blocks ... 223
Chapter 1: Android Activities . . . .225
All about Activities ... 226
State your intention ... 231
The explicit intent ... 232
Using a context ... 234
The Activity Lifecycle ... 236
Lifecycle methods ... 236
Taking an activity lifecycle through its paces ... 243
Getting Results Back from an Activity ... 251
Applications Don’t Feel Left Out ... 255
Chapter 2: Intents and Intent Filters . . . .259
How to Make a Match ... 259
The parts of an intent ... 261
The parts of an intent fi lter ... 265
Table of Contents xv
Matching: The general idea using a (silly) analogy ... 266
The real story ... 267
Practice, Practice, Practice ... 282
No magic ... 286
Using a ScrollView ... 287
Defi ning a layout in Java code ... 287
Activities and Stacks ... 288
The activity stack ... 289
Fly the fl ag ... 290
Chapter 3: Services . . . .297
A Very Simple Service ... 297
The service ... 297
A client activity ... 299
Services start, stop, and start again ... 301
Running a Service at Boot Time ... 303
Starting and Binding ... 305
Talking about the Weather ... 307
A service... 307
A client ... 310
Informing the user ... 312
Binding to the service ... 314
Querying the service ... 315
Using shared preferences to restart a connection ... 316
Getting Real Weather Data ... 317
Dealing with XML ... 319
Getting info from an online server ... 321
Talking to a Service as if You’re Right Next Door ... 323
Using AIDL... 323
AIDL and Java code... 325
Chapter 4: Broadcast Receivers . . . .331
Receivers 101 ... 331
Creating a receiver on the fl y ... 333
Juggling receivers and broadcasts ... 334
How to unregister a receiver ... 335
Beyond the Fundamentals ... 339
Managing receivers ... 339
How to be a stickler ... 342
Using receiver intents ... 343
Ordered broadcasts ... 345
Stopping a broadcast in its tracks ... 346
Getting results from receivers ... 347
Using permissions and other tricks... 352
Standard Broadcasts ... 353
Chapter 5: Content Providers . . . .357
Databases: From the Stone Age to the Present Day ... 357
Working with a Database ... 359
Coding for SQLite using Android’s SDK ... 361
Details about the friendly helper class ... 364
Details about the mainstream SQLite code ... 366
Creating and Using a Content Provider ... 370
At last! A content provider!... 374
The latest and greatest cursor code ... 378
Book IV: Programming Cool Phone Features ... 381
Chapter 1: Lay Out Your Stuff . . . .383
Android Layouts ... 383
Linear Layout ... 384
Attributes (A Detour) ... 386
android:layout_width and android:layout_length ... 386
android:padding and android:margin ... 392
android:gravity and android:layout_gravity ... 394
android:color ... 396
android:visibility ... 398
Relative Layout ... 398
Table Layout ... 402
Frame Layout ... 403
Chapter 2: Menus, Lists, and Notifi cations . . . .407
All about Menus ... 407
Creating an Options Menu ... 409
Defi ning the XML fi le... 410
Handling user actions ... 411
Creating a reminder ... 412
Putting the new reminder in a list ... 414
Creating a Context Menu ... 416
Making the context menu appear ... 416
Handling context menu item selections ... 418
More Stuff about Lists ... 419
Creating a list activity ... 419
A client for the list activity ... 421
Displaying Two (or More) Values in a List ... 423
Notifying the User ... 426
Notify the user on any device ... 427
Notify the user on Honeycomb and beyond ... 431
Table of Contents xvii
Chapter 3: An Android Potpourri . . . .433
Making Phone Calls ... 433
Two ways to initiate a call ... 434
Oops! No phone ... 438
On being a dialer ... 440
Keep an eye on the phone ... 440
Sending a Text Message ... 441
Working with Device Sensors ... 443
Quantifying location and orientation ... 444
Sending location and orientation ... 445
Drawing, Dragging, and Zooming ... 451
The big picture ... 451
The details ... 455
On the Importance of Waiting Patiently ... 457
Creating an AsyncTask ... 458
Using a progress bar... 460
Using an AsyncTask ... 461
Chapter 4: Apps for Tablets . . . .465
What Fragments Can Do For You ... 466
Programming with fragments ... 467
Fragments, more fragments, and even more fragments ... 472
Getting the Best of Both Worlds ... 477
Book V: The Job Isn’t Done Until . . . ... 483
Chapter 1: Publishing Your App to the Android Market . . . .485
Preparing Your Code ... 486
Un-testing the app ... 486
Choosing Android versions ... 487
Selecting an icon and a label ... 488
Set your app’s own version code and version name... 488
Creating the APK File ... 489
Digitally signing your application ... 492
Creating a keystore ... 494
Safeguarding your keystore ... 494
Creating an Android Market Account ... 495
Pricing Your Application ... 500
The paid model ... 501
The free model ... 505
Getting Screen Shots for Your Application ... 506
Uploading Your Application to the Android Market ... 507
Watching the Installs Soar ... 512
Chapter 2: Publishing Your App to the Amazon Appstore. . . 515
Becoming an Amazon Appstore Developer ... 515
Uploading an App ... 517
Book VI: Alternative Android Development Techniques ... 525
Chapter 1: Creating Code Quickly with App Inventor . . . .527
Getting Started with App Inventor ... 527
Creating a Project ... 529
Using the Designer ... 534
Adding a component to your project ... 535
Setting component properties ... 537
Arranging screen elements ... 539
Using the Blocks Editor ... 544
Adding event handlers ... 544
Event handlers with parameters ... 550
Chapter 2: More App Inventor Magic . . . .557
Snap a Photo ... 557
Send a Text Message ... 559
Travel to the Orient ... 561
Animate! ... 564
Make sprites bounce off the edges of the screen ... 567
Make sprites bounce away from each other ... 569
Using a Database ... 571
Starting Another Android App ... 576
Activities and intents... 576
Starting an activity with App Inventor ... 577
Chapter 3: How to “Rough It” without Eclipse . . . .581
Preliminaries ... 581
Your friend, the command window ... 582
Scripting ... 583
Meet Apache Ant ... 584
An Ant’s-eye view ... 584
Installing Apache Ant ... 586
Getting Ready for Text-Based Development ... 587
Preparing your system ... 588
Creating a project ... 592
Android Development with Ant ... 595
Android Development with Operating System Commands ... 597
Table of Contents xix
Chapter 4: Going Native . . . .605
The Native Development Kit ... 606
Getting the NDK ... 606
Getting a C compiler ... 607
Creating an Application ... 610
Index ... 625