Upload
doannhu
View
216
Download
0
Embed Size (px)
Citation preview
«compl*tcIDIOT'SGUIDE
Android AppDevelopment
by Christopher Froehlich
AALPHA
Amember of Penguin Group (USA) Inc.
Contents
Part 1: Getting Started 1
1 An Open Invitation 3
Starting from Scratch 3
Software 4
Programming Experience 4
Selecting Your Development Environment 5
Varieties of Devices 6
Selecting a Platform V
Device Restrictions 8
Forward Thinking and Backwards Compatibility 9
Assembling Your Toolkit 9
Java 10
Eclipse IDEfor Java Developers 10
Android SDK U
The ADTPluginfor Eclipse 12
Registeringfor the Android Market 14
2 Building for Android 15
Android Development Overview 15
Introduction to Eclipse 16
The Role ofthe Android SDK 18
Starting Your First Project 18
Creating the ProjectFiles 19
Inside Your Project 21
Setting Your Preferences 22
Aloha, Android 24
Meet the Emulator 25
Debugging with the Emulator 27
Making the Device Connection 28
3 Crafting the Layout 31
Application Fundamentals 32
Reintroducing the Recipe App 34
Laying Out the Welcome Screen 34
Expanding Your View 40
Formattingfor All Displays 46
Your Icon 47
Introducing Your First Error 49
iv The Complete Idiot's Guide to Android App Development
4 Finding More Activities
Improving Your Recipe App —• ^
Introduction to Refactoring^
About Vow App$6
Updating the Android Manifest ^
Adding an Activity .
$8
Creating the Layout$9
Putting It Together62
Styling Your App65
Selecting a Theme 65
Rolling Your Own Style 67
Polishing Your App68
Adding a Menu68
Storing Some Settings. 71
Quitting Time75
5 Programming for Android 77
What Is Java?77
Object Oriented Programming Review 78
The Java Virtual Machine 79
The Front of an Android Application 80
Activities 80
Views ,
82
Behind the Scenes 86
Intents 86
Intent Filters 88
Background Receivers 89
Part 2: Constructing Your Application 91
6 Resources and Animation 93
The Splash View 93
Assemble the View 94
Colors and Dimensions 96
Ready, Set, Animate 99
Method Acting 101
2D Graphics Overview 106
Working with Dratvables 107
Using Shapes. 107
Contents v
7 Building Input and Output HI
Making a New Recipe HI
EditText and Other Widgets •112
Deep Nested Views 115
Working with ListViews 120
Inflating the Layout 120
FromXML toJava 123
Simplify the Interface with TabHosts 127
Toast Feedback 131
8 Storing and Retrieving Data 135
Storage Overview 135
SbaredPreferences 136
Internal Storage •138
External Data 140
Saving a New Recipe 140
Retrieving SbaredPreferences 141
Storing Input as SbaredPreferences 148
Manage Your Recipes 151
9 Search for It 157
Search Basics 157
Create the Configuration 159
Define a Searchable Activity 161
Build the Search Activity 162
Providing the Search Option 165
Voice Search 166
More Search Options 167
10 From Widgets to the Browser 169
Widgets and Dialogs 169
Constructing a Dialog 171
Dates and Times 173
More Widgets and Controls 176
Integrating the Browser 179
Opening the Web Page 180
Linkify 1%2
Embedding the Browser 183
vi The Complete Idiot's Guide to Android App Development
Part 3: Make the Most of the Hardware 189
11 Cameras and Media 191
Introduction to Multimedia on Android 191
Incorporating the Camera into Your App 193
Prepare the SurfaceView 193
Working the Camera Class 195
Capturing the Photo 197
Recording Video 198
Accessing Stored Photos 200
Embedding Audio or Video 201
Using the Microphone 202
12 Location-Based Services 205
Introduction to Android Sensors and Receivers 205
Location, Location 207
Location Services 208
Consider Accuracy and the Battery 212
Emulator Limitations 213
Sense and Sensors 214
Reading a Sensor 215
Sensor Data 218
Integrate Maps 219
Registerfor Google Maps Access 219
Working with Google Maps 220
13 3D Graphics and Animation 225
Introduction to 3D Graphics 225
OpenGL Basics 226
Drawing an Activity 228
Model Objects 230
The Cube 230
Moving the Cube in 3D 233
14 Core Services 239
Overview of Android Hardware 239
The Phone Itself 240
Working with Calls 241
Send SMS and MMS Messages 245
Contents vii
Network Interfaces 247
Wi-Fi vs. 3G 248
Wi-Fi 250
Working with Bluetooth 251
Part 4: Increasing Your Application Scope 259
15 A Touch of Locale 261
Localization 261
Managing Resources 262
Alternate Resources 264
Performing Translation in Code 267
Runtime Changes 270
Efficiently Rotating the Screen 272
Manually Manage Configuration Changes 273
Multi-Touch 274
MotionBvents 275
Build Your Own Gestures 277
16 Threads and the Background 283
What Are Threads? 283
About Processes. 285
Working with Asy?jcTask 286
Dispatching Threads 290
Services 293
The Service Life Cycle 294
Building a Basic Service 294
Using Notifications 297
17 SQL and Databases 301
SQL at a Glance 301
Intro to SQLite 304
Building the Helper Class 305
Database Interaction 307
More SQL Detail 310
Bring the Data into View 311
Additional SQLite Tools 315
viii The Complete Idiot's Guide to Android App Development
18 ContentProviders 319
Introduction to ContentProviders 319
Provider Syntax. •
Requesting Datafrom a ContentProvider 322
Building Your Own Provider 323
Prepare the Class.., ^24
Implement the Methods 326
Provider Permissions and the Manifest 330
Part 5: Taking Your App to Market 333
19 Comprehensive Debugging 335
Logging 335
Debugging Your App • 338
The Debug Perspective 339
Advanced Logging-with Tracevievi 340
Switching to the DDMS Perspective 341
Debugging with Dev Took 343
20 Testing Your Apps 345
The Good News 345
Build a Testing Plan 346
Selecting Your Targets 341
Working ivith Multiple Versions ofAndroid 348
Methods Change 350
Be Mindful ofthe Future 352
And More Tests 353
21 App Markets and Beyond 355
Available Android Markets 355
Welcome to the Android Market 357
Registering Your Accounts 557
App Preparation 358
Certification 360
And Release! 362
Next Steps 364
Advertising 365
The Next Generation 366
Keep in Touch 366
Contents ix
Appendixes
A Glossary . 369
B Resources, References, and Useful Websites....375
Index 379