android wireless application development · a new and growing platform : 23 the android platform 23...
Post on 07-Oct-2020
5 Views
Preview:
TRANSCRIPT
Android WirelessApplication
Development
Shane ConderLauren Darcey
r\Addison-WesleyT T
Upper Saddle River, NJ • Boston • Indianapolis • San FranciscoNew York • Toronto • Montreal • London • Munich • Paris • Madrid
Cape Town • Sydney • Tokyo • Singapore • Mexico City
Table of Contents
Introduction 1 :
Who: Should Read This Book 1 ; ' \
Key Questions Answered in this Book 2 1
How This Book Is Structured 2 ;
Android Development Environment Used in This Book
Supplementary Materials Available 4;
Where to Find More Information 4 i •
Conventions Used in this Book 5 ;
Contacting the Authors 5
An Overview of Android T
1 Introducing Android 9
A Brief History of Mobile Software Development 9
Way Back When 9
"The Brick" 11 ;
Wireless Application Protocol (WAP); 12 :
Proprietary Mobile Platforms 14 • •• • ;
the Open Handset Alliance 16 ,
Google Goes Wireless 17
Forming of the Open Handset Alliance 17
Manufacturers: Designing the Android Handsets 18
Content Providers:- Developing Android ,' Applications i 8 ; ;
Mobile Operators: ̂ Delivering the AndroidExperience 18 -
Taking Advantage of All Android Has to Offer 18
Android Platform Differences 19 ! , .
Android: A Next Generation Platform 19 "'
Free and Open Source 20 ;
Familiar and Inexpensive Development Tools 20
Reasonable Learning Curve for Developers 21
Enabling Development of Powerful Applications 21
Rich, Secure Application Integration; 21
No Costly Obstacles to Publication 22
A "Free Market" for Applications 22 ;
A New and Growing Platform : 23
The Android Platform 23 i
Android's Underlying Architecture 24
Security and Permissions 26
Developing Android Applications . 27
Summary 29 ; . . ' t ;
References and More Information 29
2 Your Android Development Environment 3 1
Configuring Youf Development Environment 31 !
• Supported Operating Systems 31 ; . • i
Installing the Java Development Kit 32 ;
Installing the Eclipse Development Environment forJava 32 • ! ; . ] . ' ' .
Other Development Environments 33 •
Installing the Android Software Development Kit 34
• Installing and Configuring the Android Plug-In for• Eclipse (ADT) : 35 ' ;
Troubleshooting the ADT Installation 37
Upgrading the Android Software Development Kit 37
- Problems with the Android Software ;Development Kit 38
Configuring Your Android Hardware for Debugging 38
Configuring Your Operating System for Device ;
Debugging 38 / ' ; \ \
The ^Development Environment Used in'This Book 39
Exploring the Android Software Development Kit 39
The Android SDK License Agreement 39; The Android SDK Documentation 41
: The Android Application Framework 42
The Android Tools 43 :
'-, The Android Sample Applications 47
Summary 47 i i •
References and More Information 47
II:
Writing Your First Android Application 49
Testing Your development Environment '• 49
Adding the Snake Application to a Project in YourEclipse Workspace 50
Creating an Android Virtual Device (AVD) for Your• Snake Project ;52 \ ;
Creating a! Launch Configuration for Your *Snake Project 52 ;
Running the Snake Application in the Androidi Emulator 54 . ' > ;
Building Your First Android Application 55
Creating and Configuring a New Android Project 55
Core Filesartd Directories of the Android •:1 Application 56
Creating an AVD for Your Project 57 ; ;
Creating a! Launch Configuration for Your Project 58
Running Your Android Application in the Emulator 58
Debugging Your Android Application in the] Emulator 61 !
Adding Logging Support to Your Android [", Application 63
Adding Some Media Support to Your Application 64
Adding Some Location-Based Services Supporti to Your Application 66 I
debugging Your Application on the Hardware 68
Summary 69 . • ; ;
References and More Information 69
Android Application Design Essentials
4 Understanding the Anatomy of an AndroidApplication 73
The Life Cycle of an Android Application 73
Performing Application Tasks with Activities . 73
Transitioning Between Activities with intents 74
The Lifecycle of an Android Activity 77
Defining Your Application Using the AndroidManifest File 82
Managing Your Application's Identity 85
Registering Activities and Other ApplicationComponents 86 : |
Working with Permissions 88
Specifying Application input Hardware and Software; Requirements 89 ' '<
Working with libraries; and Android SDK Versions 91
Defining Other Application Configuratiori Settingsin the Manifest File 92 ' ' '• •
S u m m a r y 9 2 *- •* • j ' ' ' . ' • ; ' I " •
5 Managing Application Resources 95 ;
; What Are Resources? 951 ;
Whefe and How Application Resources Are Stored 95
, The Resource Directory Hierarchy 96 i
Resource Value types? 96 : j , ; v
Storing Different Resource Value Types; 98 !•
Accessing Resources iProgrammatically 99 ,' ;
; An Example: Setting Simple Resource Values UsingEclipse 99j ' ' ;
I Working with Resources \ 103 . ' • . : .
i Working with s t r i n g "Resources 103
Using s t r i n g Resources as Format Strings 104
• Wording with s t r i n g jArrays 105 :
Working with Colors 106 j ' j • ;
Working with Dimensions 106 i
Working with Simple Drawables 107
; Working with Images. 108 ; j
Working with; Animation 113 1 i
• Working with Menus ; 114 • :
Working with XML Files 115 i 1- ;
Working with! Raw Files 117,
y References to Resources 117
Working with Layouts 118 - i ;
; Working with'Styles 1123 :
Working with Themes! 126 ; ;
Referencing System Resources ; 126 -t ;., . :
Managing Multiple Application Configurations 127
Specifying* Resources for Localization andDevice Configurations 129
Using Appropriate Configuration Resources; Prografnmatically 131 . ,
Organizing Application Resources Efficiently ' 134
Summary 135
References and More Information 135
111: Android User Interface Design Essentials
6 Exploring User interface Screen Elements 139
- Introducing Android Views, Widgets, and Layouts 139
Introducing the Android View 139;
Introducing the Android Widget 139
Introducing the Android Layout 140 •• <.
Displaying Text to Users with textview 1401
Configuring Layout and Sizing 1 4 i
Creating Contextual Links in Text 142
Getting Text From Users with EditText; and Spinner 144
Helping the User with Auto Completion 146
Constraining User Input with Input Filters 149
Constraining User Input with Drop-Down Lists UsingI Spinner Widgets 150 i
Using Buttons, Check Boxes, and Radio Groups 152
Using Basic Buttons 153
Using Check Boxes and Toggle Buttons 154
Using RadioGroups and RadioButtons 155
Getting Dates and Times from Users '. 157
Indicating Information to Users 159; ;
Indicating Progress with ProgressBar 159
indicating and Adjusting Progress with seekBar 161
Indicating and Adjusting Ratings with RatingBar 162
indicating the Passage of Time with the •Chronometer 163 .;
Indicating the Current Time with the Analogciock andDig i ta lC lock 165
Providing Users with Options and Context Menus 166
Enabling the Options Menu 166
Enabling the Context Menu 168
Handling User Events 170 ; ; ',
Listening for Touch Mode Changes 170
Listening for Events on the Entire Screen 171
Listening for Long Clicks 172 ;
Listening for Gestures 173
Listening for Focus Changes 175
Listening for Screen Orientation;Changes 176
Working with Styles 177 i \ ;
Working with Themes 179 j
Summary 181 ; I '
Designing Android User interfaces with Layouts 183
Creating User Interfaces in Android: 183 ! •
Creating Layouts Using XML Resources i 183 :
Creating Layouts Programmatieally 185 .;.
Organizing Your User Interface with ViewGroups 187Understanding the Relationship Between view and
ViewGroup 187 : '
Using Built-in Layout Classes 191 ! ;
Using AbsoluteLayout 193 *!
Using FrameLayout 195 ;
Using LinearLayout 197
Using; Relat iveLayout • 199
Using; TableLayout 202 ; -
Using Multiple Layouts on a Screen 204 ;
Using Built-in view Container Classes 205
Using Data Driven view Containers with i ' .
-, Adapterviews 206
Organizing Views with TabAct iv i ty and TabHost 210
* Exploring Other View Containers 212
Using AppWidgets to Expose Application Views 215
Becoming an AppWidget Provider 217 '.
Becoming an AppWidget Host 217
Summary 217 ;
8 Drawing and Working with Animation inAndroid 219 ;
Drawing on the Screen 219 ; ;
Working with Canvases and Paints • 219
j Working with Text 224 ,
Using Default Fonts and Typefaces ; 224 :
\ Using Custom Typefaces 225 J
; Measuring Text Screen Requirements 227 '-.
\ Working with.Bitmaps 227; Drawing Bitmap Graphics on a Canvas 227
j Scaling Bitmap Graphics 227 •
; Transforming Bitmaps Using Matrixes 227
j Working with Shapes 229 *
r Defining Shape Drawables as XML Resources 229
i Defining Shape Drawables Programmatically. 230
i Drawing Different Shapes 231 j
Working with Animation 238 '
Working with Frame-by-Frame Animation 238
Working With Tweened Animations J240
Summary 245 , < • ,
IV: Using Common Android APIs
9 Using Android Data and Storage APIs 249
Working with Application Preferences 249
Creating Private artd Shared Preferences 250
Searching and Reading Preferences 250
Adding, Updating, and Deleting Preferences 251
Finding Preferences Data on the Android ;; File System 252
Working with Files and Directories 253
Exploring with the Android Applicationi Directories 253 \
Creating and Writing to Files to the DefaultApplication Directory 254 ;
Reading from Files in the Default ApplicationDirectory 255 ;
Working with Otlier Directories and Files on the Android ;File System : 256 , ' ; \
* Storing Structured Data Using SQLite Databases 257 i
Creating a SQLite Database 258 \
, Creating, Updating, and Deleting Database ;
; Records 260 ; : ; !
;i Querying SQLite Databases 263 |
I , Closing and Deleting a SQLite Database 268 j
Designing Persistent Databases * 269 ; \
I Binding*Data" to the Application User Interface 271 i
: Sharing Data Between Applications with Content j. Providers 276; i ; :
: Leveraging a Content Provider to Access Images ! >:-h . .. . ; on trie Device 276 ; j •: : :
•'; ' . : Exploring Some of Android's Built-in Content ' !
j ; Providers ?81 ; j !
]'•', Modifying Data in Content Providers 288 • ;
; Extending Your Android Application with a Custom j
•;. : Content Provider 291 ; I
\ : Inheriting from a Content Provider 291 I
: Defining the Data URI 292 i
I ; Defining Data Columns : 292 • ' j
;'• Implementing query() , ' i n s e r t ( ) , update() , !I : d e l e t e ( ) , and get*ype() ;293 1 i| Updating the Manifest File 298 ! ; !
• Working with Live Folders 299 i I
; Summary; 301 ; ' •; i
f , References and More Information; 302 : j
; 10 Using Android Networking APIs 303 j
j ! Aeftessingithe Internet (HTTP) 303 \
,j Reading Data from the Web 304 i i
i\ Using HttpORLGonnection 305 }
\ * Parsing XML from the Network 305 !
.;] Using Threads for Network Calls 307 i
31 Displaying Images from a Network Resource 310 !
! l Retrieving Android Netwdrk Status 312 j
[ Browsing the Web with WebView 314 i
Summary 317 l . ; . !
References and More Information 318 ;
1 1 Using Location-Based Services (LBS) APIs 319
Using Global Positioning Services (GPS) 319
Finding Your Location 319 :
Locating Your Emulator 321 •
Geocoding Locations 321
Mapping Locations 325 ' '[
Mapping Intents 325 :
Mapping Views 326
Getting Your Debug API Key 328
Panning the Map View 329 i
Zooming the Map View 330 J
Marking the Spot ; 331 ' {
Doing More With Location-Based Services 336
Sumhiary 337 j
References and More Information 337 :
12 Using Android Multimedia APIs 339
Working with Still Images 339
Capturing Still Images Using the Camera 339
. ;•' Sharing Images 344 ;
i Assigning Images as Wallpapers 345
; Working with iVideo 345
; Recording; Video 346 '
' Playing Video 347
} Working with Audio 349 • ' ' • i
:'• Recording Audio 349
Playing Audio 350
; Sharing Audio 351
- Working with Ringtones 352 !
\ Summary 352 '
13 Using Android Telephony APIs 353
Working with Telephony Utilities 353 ;
Gaining Permission to Access Phone State :
; Information 354
; Requesting Call State 354
Requesting Service Information 356
Working with Phone Numbers 356
Using SMS 358 !
Gaining Permission to Send and Receive SMSMessages 358
Sending an SMS 358
Receiving an SMS 360;
Making Phone Calls 363
Summary '364
References and More Information; 364
14 Using Android 3D Graphics with OpenGL ES 365
Defining OpenGL ES 365 j •; '. :
Using OpenGL ES for 3D Applications with Android 366
Creating Your SiirfaceView . 367 ,, • :,
Starting Your OpjenGL ES1 Thread 368
: Initializing EGL \ 370
t Initializing GL 372
i Drawing 372 J.:
Drawing 3D Objects; 374
j Drawing Your Vertices , 374
: ; Coloring Your Vertices 375
; Drawing More Complex Objects 376
Lighting Your Scene 378
Texturing Your Objects 380 ;
Interacting with Android Views and Events 382
Enabling the OpenGL Thread to Talk to thejApplication Thread 383 ' !
''Enabling the Application Thread to Talk to theOpenGL Thread 384 ; •
Cleaning Up OpettGL ES 386 ;
# Using the GLSurf aceview Class 386
Summary ; 389 •
References and iMore Information 389
15 Using Android's Optional Hardware APIs
Using the Device Sensor 391 r
391
Working with WIFi 395 ; s.
Monitoring the Battery 397
Summary 400
V: More!Android Application Design Principles
16 Working with Notifications 403
Notifying with the Status Bar 403
Vibrating the Phone .407
Blinking the Lights 408 ; . ;
Mailing Noise 410 1
Customizing the Notification 410
Surhmary 412 , ;
17 Working with Services 413
Creating a Service (413 i \
Controlling a Service' 417
Implementing a Remote Interface 418
Implementing a Parcelable Class 420 ?
Summary 423
VI: Deploying Your Android Application to the World
18 The Mobile Software Development Process 427
An Overview; of the Mobile Development Process 427
Choosing a Software Methodology 428
Understanding the Dangers of Waterfall
Approaches 428 *
Understanding the Value of Iteration 429
Gathering Application Requirements 429
Determining Project Requirements 429
Developing Use Cases for Mobile Applications 430
Incorporating Third-Party Requirements 431
Managing a Handset Database 431
Assessing Project Risks 434 •
Identifying Target Handsets 434
Acquiring Target Handsets 435 :
i Determining Feasibility of Application ;. : Requirements 436
• Understanding Quality Assurance Risks 436
Writing Essential Project Documentation 437 ;
Developing Test Plans for Quality AssurancePurposes 438 I .;
Providing Documentation Required by ThirdParties 438 ;
Providing Documentation for Maintenance and PortingPurposes 438
Implementing Configuration Management Systems for
' Mobile Applications 439 i
Choosing a Source Control System 439
Implerhenting a Version System that Works 430
Designing Mobile Applications 440 \
Understanding Mobile Device Limitations 440
, Exploring Common Mobile Application-i Architectures 440! i
, Designing for Extensibility and Maintenance 441
Designing for Application Interoperability ; 442 '
: Developing Mobile Applications 442 '
: Testing Mobile Applications 442
Deploying Mobile Applications 443
Determining Target Markets 443 ']••
Including Market Requirements in Application ;
Requirements 443 i
; Supporting and Maintaining Mobile Applications 444
Maintaining Adequate Application Documentation 444
Managing Live Server Changes ; 444
Identifying Low-Risk Porting Opportunities 444
Summary 445 ; '
References and More Information 445
' 19 Developing and Testing Bulletproof AndroidApplications 447
Best Practices in Designing Bulletproof MobileApplications 447 ',
Meeting Mobile Users' Demands 448 '
Designing User Interfaces for Mobile Devices 448
Designing Stable and Responsive Mobile; Applications 450 «
Designing; Secure Mobile Applications 451
Designing1 Mobile Applications for MaximumProfit -452 i - '', ,
Leveraging Third-Party Standards for Android;; Application Design 453 ;
Designing Mobile Applications for Ease of Maintenance; and Upgrades 453 ;
Leveraging Android Tools for Android Application! Design; 455 I •
Avoiding Silly Mistakes in Android Application ;i Design 455 : j ; ;
Best Practices in Developing Bulletproof Mobile;Applications 456 *
Designing a Development Process That Works forMobile Development 456
Testing the Feasibility of Your Application Early.: I and Often 456 f
Using Coding Standards, Reviews, and Unit Tests to ;j Improve Code Quality 457 ;
Handling Defects Occurring on a Single Handset 459
Leveraging Android Tools for Android Application; Development 460 ;
Avoiding Silly Mistakes in Android ApplicationDevelopment 460 \
Best Practices in Testing Mobile Applications 460
(Designing a Mobile Application Defect Tracking: System 461
Managing the Testing Environment 462
Determining Clean Starting State on a Device : 463
Maximizing Testing Coverage 464 •
Leveraging Android Tools for Android ApplicationTesting 470
Avoiding Silly Mistakes in Android Application; Testing 470 ; •
Outsourcing Testing Responsibilities 471
Summary 471 ;
References and More Information 471
20 Selling Your Android Application 473
Packaging Your Application for Publication 473 ;
Preparing Your Code to Package 474 ; :
Preparing Your Package 475 ;
Testing the Release Version of Your ApplicationPackage. 477 ;
Certifying Your Android Application 477 ;
Making Money 478 ; '•'•'•
Selling Your Application on the Android Market 478
Selling Your Application on Ydur Own Server 484
Selling Your Application Using Other Alternatives 485
Protecting Your Intellectual Property ; 485 • \
Billing the User 486 . ;
Summary 486 ; j :
References and More Information 487 ;
VII: Appendixes ;
A The Android Emulator Quick-Start Guide 491
Getting Up to Speed Using the Default Emulator 492
Managing Emulator Configuration with Android Virtual ;Devices (AVDs) 494 .
Listing Android Target Operating Systems 494 , ';
Creating AVDs 495 ! i ,
Listing All Known AVDs 501 ;
Deleting AVDs 501 ' i
Using Other AVD Commands, 501
• Controlling Emulator Behavior Using Startup Options 502
Configuring Emulator Startup Options' from the' Command Line 503
Configuring Emulator Startup Options Using; Eclipse 503
# Configuring the Location Information of the Emulator 504
Calling Between Two Emulator Instances 505 ',
Messaging Between Two Emulator Instances 507 i
Interacting with the Emulator Through the Console 507
Using the Console to Simulate Incoming Calls 507
Using the Console to Simulate SMS Messages 508
Using the Console to Send GPS Coordinates 509
Using the Console to Monitor Network Status 510
Using the Console to Manipulate Power Settings 510
Using Other Console Commands 511
Enjoying the Emulator; 511 I
Understanding Emulator Limitations 512
B The Android DDMS Quick-Start Guide 513
; Using DDMS with Eclipse and as a Stand-Alone \Application 513
Getting Up to Speed Using Key Features of DDMS 515
•: Working with Processes 515 :
[ Attaching a Debugger to an Android Application 516
1 Monitoring Thread Activity of an Android [\ ; Application 517 i
Prompting Garbage Collection (GC) on an Android ,; Application 5 i 7
Monitoring Heap Activity of an AndroidApplication 517
Stopping an Android Process 518
; Working with ;the File Explorer 518 ;
j Copying Files from (the Emulator or Device 519
Copying Files to the Emulator or Device 519
Deleting Files on the Emulator or Device 520
Working with the Emulator Control 520
Simulating Incoming Voice Calls 521
, Simulating Incoming SMS Messages 521
j Sending a Location Fix 521
j Working with .Application Logging 522
;• Taking Screen Captures of Emulator and Device: Screens 523 "
C The Android Debug Bridge Quick-Start Guide
; Listing Devices and Emulators Connected to aMachine 525
525
Directing ADB Commands to Specific Devices 526
Starting and Stopping the ADB Server Process 526
Stopping the ADB Server Process : 526 «
Starting and Checking the ADB Server Process 526
Copying Files to and from Android Applications )
•Using ADB 527i . : j ,. .; : j
SendingiFiles to a Device; 527 \
Retrieving Files from a Device 527 ; i
Installing and Uninstalling Android Applications \
Using ADB 527 ; ! j
Installing Applications Using ADB 528 ;
fleinstalfing Applications ijlsing ADB 528
;Uninstalling Applications Using ADB 528
Working with LogCat Logging 529 :
Displaying All Log Information 529 - ; -Changing Logging Modes* to Include the Date
and Time 529 ;
Filtering Log Information j 529
Clearing the Log ; 530 j
iRedirecting Log Output to ]a File
Accessing the Secondary Logs ; 531
Generating Bug Reports 531 T ,
Issuing Shell Commands 531
Issuing a Single Shell Command; ;532
Starting and Using a Shell Session 532
Using the ADB Shell to Start and* Stop the { *: Emulator 532 i * i jUsing the ADB Shell to Inspect SQLite Databases 532
Using the ADB Sfieli to Stress Test Applications UsingMonkey 533; ', ' " i
Installing and Using Custom Binaries via the Shell 535
Exploring Other ADB Commands 537 •
D The SQLite Quick-Start Guide 539
Exploring Common Tasks with SQLite 539 :
Using the s q l i t e 3 Command-Line Interface 540 :
Launching the s q i i t e 3 Command-Line Interface andConnecting to a Database 540 • :
Exploring Your Database 541 • ;
Importing and Exporting the Database and Its; Data 542
Executing SQL Commands on the Command Line 544
Poking Around Within SQLite Internals 544
Using Otfier sq l i t e3 Commands 545
IJJhderstanding SQLite Limitations > 545
Understanding SQLite By Example: Student GradeDatabase} 545 i I . ;, •
Designing the Student Grade Database Schema 546
Creating Simple Tables with AUTOINCREMENT 546'
Inserting Data into Tables ; 547 ;
Querying Tables for Results with SELECT 547
Creating Tables with Foreign Keys and CompositePrimary Keys 548 ; •
Altering and Updating Data in Tables 549Querying for Information Stored in Multiple Tables
Using LJOIN 550 • :
Using Calculated Columns 550 -. • • j
Using Sufaqueries Jfor Calculated Columns 552
Deleting Tables 552 | ' ;
Index 553 ;
top related