资源说明:Contents
Acknowledgments xxxi
About the Authors xxxiii
Introduction 1
Who Should Read This Book 1
Key Questions Answered in This Book 2
How This Book Is Structured 2
An Overview of Changes in This Edition 3
Development Environments Used in This Book 5
Supplementary Materials Available 6
Where to Find More Information 6
Conventions Used in This Book 7
Contacting the Authors 7
I: An Overview of the Android Platform
1 Introducing Android 11
A Brief History of Mobile Software Development 11
Way Back When 11
“The Brick” 13
Wireless Application Protocol (WAP) 15
Proprietary Mobile Platforms 17
The Open Handset Alliance 18
Google Goes Wireless 18
Forming the Open Handset Alliance 19
Manufacturers: Designing Android Devices 19
Mobile Operators: Delivering the Android
Experience 20
Apps Drive Device Sales: Developing Android
Applications 21
Taking Advantage of All Android Has to Offer 22
The Android Marketplace: Where We Are Now 22
Android Platform Differences 23
Android: A Next-Generation Platform 23
Free and Open Source 25
Familiar and Inexpensive Development Tools 25xii Contents
Reasonable Learning Curve for Developers 26
Enabling Development of Powerful Applications 26
Rich, Secure Application Integration 26
No Costly Obstacles for Development 27
A “Free Market” for Applications 27
A Growing Platform 28
The Android Platform 29
Android’s Underlying Architecture 29
Security and Permissions 31
Exploring Android Applications 32
Summary 36
Quiz Questions 36
Exercises 36
References and More Information 36
2 Setting Up Your Android Development
Environment 37
Configuring Your Development Environment 37
Configuring Your Operating System for Device
Debugging 39
Configuring Your Android Hardware
for Debugging 39
Upgrading the Android SDK 42
Problems with the Android Software
Development Kit 42
Exploring the Android SDK 43
Understanding the Android SDK License
Agreement 43
Reading the Android SDK Documentation 44
Exploring the Core Android Application
Framework 45
Exploring the Core Android Tools 47
Exploring the Android Sample Applications 51
Summary 52
Quiz Questions 52
Exercises 53
References and More Information 53 Contents xiii
3 Writing Your First Android Application 55
Testing Your Development Environment 55
Adding the Android Samples Using the SDK
Manager 56
Adding the Snake Project to Your Android IDE
Workspace 57
Creating an AVD for Your Snake Project 61
Creating a Launch Configuration for Your Snake
Project 62
Running the Snake Application in the Android
Emulator 66
Building Your First Android Application 68
Creating and Configuring a New Android Project 69
Core Files and Directories of the Android
Application 73
Creating an AVD for Your Project 75
Creating a Launch Configuration for Your Project 75
Running Your Android Application in the Emulator 76
Debugging Your Android Application in the
Emulator 80
Adding Logging Support to Your Android Application 83
Adding Some Media Support to Your Application 84
Adding Location-Based Services to Your Application 88
Debugging Your Application on Hardware 90
Summary 92
Quiz Questions 93
Exercises 94
References and More Information 94
II: Android Application Basics
4 Understanding the Anatomy of an Android
Application 97
Mastering Important Android Terminology 97
The Application Context 98
Retrieving the Application Context 98
Using the Application Context 98
Performing Application Tasks with Activities 99
The Lifecycle of an Android Activity 100xiv Contents
Organizing Activity Components with
Fragments 105
Managing Activity Transitions with Intents 106
Transitioning between Activities with Intents 106
Organizing Application Navigation with Activities and
Intents 110
Working with Services 110
Receiving and Broadcasting Intents 111
Summary 112
Quiz Questions 112
Exercises 112
References and More Information 113
5 Defining Your Application Using the Android
Manifest File 115
Configuring Android Applications Using
the Android Manifest File 115
Editing the Android Manifest File 116
Managing Your Application’s Identity 122
Versioning Your Application 122
Setting the Application Name and Icon 122
Enforcing Application System Requirements 123
Targeting Specific SDK Versions 123
Enforcing Application Platform Requirements 126
Working with External Libraries 128
Other Application Configuration Settings and
Filters 129
Registering Activities in the Android Manifest 129
Designating a Primary Entry Point Activity for Your
Application Using an Intent Filter 130
Configuring Other Intent Filters 130
Registering Other Application Components 131
Working with Permissions 131
Registering Permissions Your Application
Requires 131
Registering Permissions Your Application
Enforces 132 Contents xv
Exploring Other Manifest File Settings 133
Summary 133
Quiz Questions 134
Exercises 134
References and More Information 135
6 Managing Application Resources 137
What Are Resources? 137
Storing Application Resources 137
Resource Value Types 138
Accessing Resources Programmatically 142
Setting Simple Resource Values
Using the Android IDE 143
Working with Different Types of Resources 146
Working with String Resources 147
Using String Resources as Format Strings 148
Working with Quantity Strings 149
Working with String Arrays 150
Working with Boolean Resources 151
Working with Integer Resources 152
Working with Colors 152
Working with Dimensions 153
Drawable Resources 154
Working with Images 156
Working with Color State Lists 158
Working with Animation 159
Working with Menus 162
Working with XML Files 163
Working with Raw Files 164
References to Resources 165
Working with Layouts 166
Referencing System Resources 171
Summary 172
Quiz Questions 173
Exercises 173
References and More Information 173xvi Contents
III: Android User Interface Design Essentials
7 Exploring User Interface Building Blocks 177
Introducing Android Views and Layouts 177
The Android View 177
The Android Controls 177
The Android Layout 178
Displaying Text to Users with TextView 179
Configuring Layout and Sizing 179
Creating Contextual Links in Text 180
Retrieving Data from Users with Text Fields 183
Retrieving Text Input Using EditText Controls 183
Constraining User Input with Input Filters 184
Helping the User with Autocompletion 186
Giving Users Choices Using Spinner Controls 188
Allowing Simple User Selections with Buttons and
Switches 190
Using Basic Buttons 190
Using CheckBox and ToggleButton
Controls 193
Using RadioGroup and RadioButton 194
Retrieving Dates, Times, and Numbers from Users
with Pickers 197
Using Indicators to Display Progress and Activity
to Users 199
Indicating Progress with ProgressBar 199
Indicating Activity with Activity Bars and Activity
Circles 202
Adjusting Progress with Seek Bars 202
Other Valuable User Interface Controls 203
Displaying Rating Data with RatingBar 204
Showing Time Passage with the
Chronometer 205
Displaying the Time 206
Summary 207
Quiz Questions 207
Exercises 207
References and More Information 208 Contents xvii
8 Designing with Layouts 209
Creating User Interfaces in Android 209
Creating Layouts Using XML Resources 209
Creating Layouts Programmatically 211
Organizing Your User Interface 214
Using ViewGroup Subclasses for Layout
Design 214
Using ViewGroup Subclasses as View
Containers 214
Using Built-in Layout Classes 215
Using LinearLayout 217
Using RelativeLayout 219
Using FrameLayout 222
Using TableLayout 224
Using GridLayout 228
Using Multiple Layouts on a Screen 230
Using Container Control Classes 232
Using Data-Driven Containers 233
Adding Scrolling Support 238
Exploring Other View Containers 239
Summary 239
Quiz Questions 239
Exercises 240
References and More Information 240
9 Partitioning the User Interface with Fragments 243
Understanding Fragments 243
Understanding the Fragment Lifecycle 244
Working with Special Types of Fragments 247
Designing Fragment-Based Applications 248
Using the Android Support Package 258
Adding Fragment Support to Legacy
Applications 259
Using Fragments in New Applications Targeting Older
Platforms 259
Linking the Android Support Package to Your
Project 260
Exploring Nested Fragments 261xviii Contents
Summary 261
Quiz Questions 262
Exercises 262
References and More Information 263
10 Displaying Dialogs 265
Choosing Your Dialog Implementation 265
Exploring the Different Types of Dialogs 266
Working with Dialogs and Dialog Fragments 267
Tracing the Lifecycle of a Dialog and
DialogFragment 268
Working with Custom Dialogs 270
Working with Support Package Dialog Fragments 271
Summary 275
Quiz Questions 276
Exercises 276
References and More Information 276
IV: Android Application Design Essentials
11 Using Android Preferences 281
Working with Application Preferences 281
Determining When Preferences Are Appropriate 281
Storing Different Types of Preference Values 282
Creating Private Preferences for Use by a Single
Activity 282
Creating Shared Preferences for Use by Multiple
Activities 282
Searching and Reading Preferences 283
Adding, Updating, and Deleting Preferences 284
Reacting to Preference Changes 285
Finding Preferences Data
on the Android File System 285
Creating Manageable User Preferences 286
Creating a Preference Resource File 287
Using the PreferenceActivity Class 289
Organizing Preferences with Headers 291
Learning about Cloud Save
for Android Applications 296 Contents xix
Summary 298
Quiz Questions 298
Exercises 299
References and More Information 299
12 Working with Files and Directories 301
Working with Application Data on a Device 301
Practicing Good File Management 302
Understanding Android File Permissions 303
Working with Files and Directories 303
Exploring with the Android Application
Directories 304
Working with Other Directories and Files
on the Android File System 309
Summary 312
Quiz Questions 312
Exercises 313
References and More Information 313
13 Leveraging Content Providers 315
Exploring Android’s Content Providers 315
Using the MediaStore Content Provider 316
Using the CallLog Content Provider 318
Using the Browser Content Provider 319
Using the CalendarContract Content
Provider 321
Using the UserDictionary Content Provider 321
Using the VoicemailContract Content
Provider 322
Using the Settings Content Provider 322
Introducing the ContactsContract Content
Providers 322
Modifying Content Provider Data 324
Adding Records 325
Updating Records 326
Deleting Records 327
Using Third-Party Content Providers 328
Summary 328
Quiz Questions 328xx Contents
Exercises 329
References and More Information 329
14 Designing Compatible Applications 331
Maximizing Application Compatibility 331
Designing User Interfaces for Compatibility 333
Working with Fragments 335
Leveraging the Android Support Library 335
Supporting Specific Screen Types 335
Working with Nine-Patch Stretchable Graphics 336
Using the “Working Square” Principle 336
Providing Alternative Application Resources 338
Understanding How Resources Are Resolved 338
Organizing Alternative Resources with Qualifiers 339
Providing Resources for Different Orientations 345
Using Alternative Resources Programmatically 345
Organizing Application Resources Efficiently 345
Targeting Tablets, TVs, and Other New Devices 347
Targeting Tablet Devices 347
Targeting Google TV Devices 348
Targeting Google Chromecast Devices 350
Summary 350
Quiz Questions 350
Exercises 351
References and More Information 351
V: Publishing and Distributing Android Applications
15 Learning the Android Software Development
Process 355
An Overview of the Mobile Development Process 355
Choosing a Software Methodology 356
Understanding the Dangers of Waterfall
Approaches 356
Understanding the Value of Iteration 357
Gathering Application Requirements 357
Determining Project Requirements 357
Developing Use Cases for Mobile Applications 360 Contents xxi
Incorporating Third-Party Requirements and
Recommendations 360
Managing a Device Database 361
Assessing Project Risks 364
Identifying Target Devices 364
Acquiring Target Devices 366
Determining the Feasibility of Application
Requirements 366
Understanding Quality Assurance Risks 367
Writing Essential Project Documentation 368
Developing Test Plans for Quality Assurance
Purposes 368
Providing Documentation Required by Third
Parties 369
Providing Documentation for Maintenance and
Porting 369
Leveraging Configuration Management Systems 369
Choosing a Source Control System 369
Implementing an Application Version System That
Works 370
Designing Mobile Applications 370
Understanding Mobile Device Limitations 370
Exploring Common Mobile Application
Architectures 371
Designing for Extensibility and Maintenance 371
Designing for Application Interoperability 372
Developing Mobile Applications 373
Testing Mobile Applications 373
Controlling the Test Release 374
Deploying Mobile Applications 374
Determining Target Markets 375
Supporting and Maintaining Mobile Applications 375
Track and Address Crashes Reported by Users 376
Testing Firmware Upgrades 376
Maintaining Adequate Application
Documentation 376
Managing Live Server Changes 376
Identifying Low-Risk Porting Opportunities 376
Application Feature Selection 377xxii Contents
Summary 377
Quiz Questions 377
Exercises 377
References and More Information 378
16 Designing and Developing Bulletproof Android
Applications 379
Best Practices in Designing Bulletproof
Mobile Applications 379
Meeting Mobile Users’ Demands 380
Designing User Interfaces for Mobile Devices 380
Designing Stable and Responsive Mobile
Applications 381
Designing Secure Mobile Applications 383
Designing Mobile Applications for Maximum
Profit 383
Following the Android Application Quality
Guidelines 384
Leveraging Third-Party Quality Standards 385
Designing Mobile Applications for Ease
of Maintenance and Upgrades 385
Leveraging Android Tools for Application Design 387
Avoiding Silly Mistakes in Android
Application Design 388
Best Practices in Developing
Bulletproof Mobile Applications 388
Designing a Development Process That Works
for Mobile Development 389
Testing the Feasibility of Your Application Early and
Often 389
Using Coding Standards, Reviews, and Unit Tests
to Improve Code Quality 390
Handling Defects Occurring on a Single Device 392
Leveraging Android Tools for Development 393
Avoiding Silly Mistakes in Android Application
Development 393
Summary 393
Quiz Questions 394
Exercises 394
References and More Information 394 Contents xxiii
17 Planning the Android Application Experience 395
Thinking about Objectives 395
User Objectives 396
Team Objectives 396
Objectives of Other Stakeholders 396
Techniques for Focusing Your Product Efforts 397
Personas 397
Entity Discovery and Organization 398
Use Cases and Use Case Diagrams 398
Charting Your Application’s Navigation 400
Android Application Navigation Scenarios 400
Launching Tasks and Navigating the Back
Stack 404
Navigating with Fragments 404
Planning Application Navigation 404
Android Navigation Design Patterns 405
Encouraging Action 408
Menus 410
Action Bars 411
Dialogs 415
Actions Originating from Your Application’s
Content 416
Communicating Your Application’s Identity 416
Designing Screen Layouts 417
Sketches 417
Wireframes 417
Design Comps 417
Reacting Properly with Visual Feedback 418
Observing Target Users for Usability 418
Mocking Up the Application 418
Testing the Release Build 419
Summary 419
Quiz Questions 420
Exercises 420
References and More Information 420 18 Testing Android Applications 423
Best Practices in Testing Mobile Applications 423
Designing a Mobile Application Defect-Tracking
System 423
Managing the Testing Environment 425
Maximizing Testing Coverage 427
Leveraging Android SDK Tools for Android Application
Testing 434
Avoiding Silly Mistakes in Android Application
Testing 435
Android Application Testing Essentials 435
Unit Testing with JUnit 436
Introducing the PasswordMatcher
Application 437
Determining What Our Tests Should Prove 441
Creating an Android Test Project 441
Writing the Tests 447
Running Your First Test Using the Android IDE 450
Analyzing the Test Results 450
Adding Additional Tests 453
More Android Automated Testing Programs
and APIs 455
Summary 457
Quiz Questions 457
Exercises 457
References and More Information 458
19 Publishing Your Android Application 459
Choosing the Right Distribution Model 459
Protecting Your Intellectual Property 460
Following the Policies of Google Play 461
Billing the User 461
Packaging Your Application for Publication 462
Preparing Your Code for Packaging 463
Packing and Signing Your Application 465
Testing the Release Version of Your Application
Package 467
Including All Required Resources 467
xxiv Contents Contents xxv
Readying Your Servers or Services 467
Distributing Your Application 467
Publishing to Google Play 468
Signing Up for Publishing to Google Play 468
Uploading Your Application to Google Play 471
Uploading Application Marketing Assets 473
Configuring Pricing and Distribution Details 474
Configuring Additional Application Options 475
Managing Other Developer Console Options 476
Publishing Your Application to Google Play 476
Managing Your Application on Google Play 476
Google Play Staged Rollouts 478
Publishing to the Google Play Private Channel 478
Translating Your Application 478
Publishing Using Other Alternatives 479
Self-Publishing Your Application 479
Summary 480
Quiz Questions 481
Exercises 481
References and More Information 481
VI: Appendixes
A Mastering the Android Development Tools 485
Using the Android Documentation 485
Leveraging the Android Emulator 489
Viewing Application Log Data with LogCat 490
Debugging Applications with DDMS 490
Using Android Debug Bridge (ADB) 490
Using the Resource Editors and UI Designer 491
Using the Android Hierarchy Viewer 493
Launching the Hierarchy Viewer 494
Working in Layout View Mode 495
Optimizing Your User Interface 496
Working in Pixel Perfect Mode 497
Working with Nine-Patch Stretchable Graphics 497
Working with Other Android Tools 500Summary 502
Quiz Questions 503
Exercises 503
References and More Information 503
B Quick-Start Guide: The Android Emulator 505
Simulating Reality: The Emulator’s Purpose 505
Working with Android Virtual Devices 507
Using the Android Virtual Device Manager 508
Creating an AVD 509
Creating AVDs with Custom Hardware Settings 510
Launching the Emulator with a Specific AVD 512
Maintaining Emulator Performance 512
Configuring Emulator Startup Options 513
Launching an Emulator to Run an Application 513
Launching an Emulator from the Android Virtual
Device Manager 515
Configuring the GPS Location of the Emulator 516
Calling between Two Emulator Instances 517
Messaging between Two Emulator Instances 518
Interacting with the Emulator
through the Console 520
Using the Console to Simulate Incoming Calls 521
Using the Console to Simulate SMS Messages 522
Using the Console to Send GPS Coordinates 523
Using the Console to Monitor Network Status 523
Using the Console to Manipulate Power
Settings 523
Using Other Console Commands 524
Enjoying the Emulator 524
Understanding Emulator Limitations 525
Summary 526
Quiz Questions 526
Exercises 527
References and More Information 527
C Quick-Start Guide: Android DDMS 529
Using DDMS with the Android IDE
and as a Standalone Application 529
xxvi Contents Contents xxvii
Getting Up to Speed Using Key Features of DDMS 530
Working with Processes, Threads, and the Heap 531
Attaching a Debugger to an Android Application 531
Stopping a Process 532
Monitoring Thread Activity of an Android
Application 532
Monitoring Heap Activity 532
Prompting Garbage Collection 534
Creating and Using an HPROF File 534
Using the Allocation Tracker 534
Viewing Network Statistics 535
Working with the File Explorer 536
Browsing the File System of an Emulator or
Device 536
Copying Files from the Emulator or Device 538
Copying Files to the Emulator or Device 539
Deleting Files on the Emulator or Device 539
Working with the Emulator Control 539
Change Telephony Status 540
Simulating Incoming Voice Calls 540
Simulating Incoming SMS Messages 540
Sending a Location Fix 541
Working with the System Information Pane 541
Taking Screen Captures of the Emulator
and Device Screens 542
Working with Application Logging 543
Summary 544
Quiz Questions 545
Exercises 545
References and More Information 545
D Android IDE and Eclipse Tips and Tricks 547
Organizing Your Android IDE Workspace 547
Integrating with Source Control Services 547
Repositioning Tabs within Perspectives 548
Maximizing Windows 548
Minimizing Windows 548
Viewing Windows Side by Side 548xxviii Contents
Viewing Two Sections of the Same File 550
Closing Unwanted Tabs 550
Keeping Windows under Control 550
Creating Custom Log Filters 551
Searching Your Project 551
Organizing Android IDE Tasks 551
Writing Code in Java 552
Using Autocomplete 552
Creating New Classes and Methods 552
Organizing Imports 553
Formatting Code 553
Renaming Almost Anything 553
Refactoring Code 554
Reorganizing Code 555
Using QuickFix 555
Providing Javadoc-Style Documentation 556
Resolving Mysterious Build Errors 556
Summary 557
Quiz Questions 557
Exercises 557
References and More Information 557
E Answers to Quiz Questions 559
Chapter 1: Introducing Android 559
Chapter 2: Setting Up Your Android
Development Environment 559
Chapter 3: Writing Your First Android Application 559
Chapter 4: Android Application Basics 560
Chapter 5: Defining Your Application Using the Android
Manifest File 560
Chapter 6: Managing Application Resources 560
Chapter 7: Exploring User Interface Building Blocks 561
Chapter 8: Designing with Layouts 561
Chapter 9: Partitioning the User Interface with
Fragments 561
Chapter 10: Displaying Dialogs 562
Chapter 11: Using Android Preferences 562
Chapter 12: Working with Files and Directories 562 Contents xxix
Chapter 13: Leveraging Content Providers 562
Chapter 14: Designing Compatible Applications 563
Chapter 15: Learning the Android Software Development
Process 563
Chapter 16: Designing and Developing Bulletproof
Android Applications 563
Chapter 17: Planning the Android Application
Experience 564
Chapter 18: Testing Android Applications 564
Chapter 19: Publishing Your Android Application 564
Appendix A: Mastering the Android Development
Tools 565
Appendix B: Quick-Start Guide: The Android
Emulator 565
Appendix C: Quick-Start Guide: Android DDMS 565
Appendix D: Android IDE and Eclipse Tips
and Tricks 566
Index 567
本源码包内暂不包含可直接显示的源代码文件,请下载源码包。