The Busy Coder’s Guide to Android Development

Author: Mark L. Murphy
Publisher: CommonsWare, LLC
Keywords: development, android, guide, coder’s
Number of Pages: 468
Published: 2009-02-06
ISBN-10: 0981678009
ISBN-13: 9780981678009

Book Description:

Welcome to the Book! Thanks! Thanks for your interest in developing applications for Android! Increasingly, people will access Internet-based services using so-called "non-traditional" means, such as mobile devices. The more we do in that space now, the more that people will help invest in that space to make it easier to build more powerful mobile applications in the future. Android is new – at the time of this writing, there are no shipping Android-powered devices – but it likely will rapidly grow in importance due to the size and scope of the Open Handset Alliance. And, most of all, thanks for your interest in this book! I sincerely hope you find it useful and at least occasionally entertaining.

 

Table of Contents

Welcome to the Warescription!..................................................................................xiii

Preface..........................................................................................................................xv

Welcome to the Book!...........................................................................................................xv

Prerequisites..........................................................................................................................xv

Warescription.......................................................................................................................xvi

Book Bug Bounty.................................................................................................................xvii

Source Code License..........................................................................................................xviii

Creative Commons and the Four-to-Free (42F) Guarantee............................................xviii

The Big Picture................................................................................................................1

What Androids Are Made Of.................................................................................................3

Activities...........................................................................................................................3

Content Providers...........................................................................................................4

Intents..............................................................................................................................4

Services.............................................................................................................................4

Stuff At Your Disposal.............................................................................................................5

Storage..............................................................................................................................5

Network............................................................................................................................5

Multimedia.......................................................................................................................5

GPS...................................................................................................................................5

Phone Services.................................................................................................................6

Project Structure............................................................................................................7

Root Contents..........................................................................................................................7

The Sweat Off Your Brow.......................................................................................................8

iii

Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License EditionAnd Now, The Rest of the Story.............................................................................................8

What You Get Out Of It.........................................................................................................9

Inside the Manifest........................................................................................................11

In The Beginning, There Was the Root, And It Was Good.................................................11

Permissions, Instrumentations, and Applications (Oh, My!).............................................12

Your Application Does Something, Right?..........................................................................13

Creating a Skeleton Application...................................................................................17

Begin at the Beginning...........................................................................................................17

The Activity............................................................................................................................18

Dissecting the Activity...........................................................................................................19

Building and Running the Activity.......................................................................................21

Using XML-Based Layouts............................................................................................23

What Is an XML-Based Layout?...........................................................................................23

Why Use XML-Based Layouts?............................................................................................24

OK, So What Does It Look Like?..........................................................................................25

What’s With the @ Signs?....................................................................................................26

And We Attach These to the Java...How?...........................................................................26

The Rest of the Story.............................................................................................................27

Employing Basic Widgets.............................................................................................29

Assigning Labels....................................................................................................................29

Button, Button, Who’s Got the Button?..............................................................................30

Fleeting Images......................................................................................................................31

Fields of Green. Or Other Colors..........................................................................................31

Just Another Box to Check....................................................................................................34

Turn the Radio Up.................................................................................................................37

It’s Quite a View....................................................................................................................39

Useful Properties...........................................................................................................39

Useful Methods..............................................................................................................39

Working with Containers.............................................................................................41

Thinking Linearly..................................................................................................................42

Concepts and Properties...............................................................................................42

Example..........................................................................................................................45

All Things Are Relative.........................................................................................................50

iv

Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License EditionConcepts and Properties...............................................................................................50

Example..........................................................................................................................53

Tabula Rasa............................................................................................................................56

Concepts and Properties...............................................................................................56

Example..........................................................................................................................59

Scrollwork..............................................................................................................................60

Using Selection Widgets...............................................................................................65

Adapting to the Circumstances............................................................................................65

Using ArrayAdapter......................................................................................................66

Other Key Adapters.......................................................................................................67

Lists of Naughty and Nice....................................................................................................68

Spin Control...........................................................................................................................70

Grid Your Lions (Or Something Like That...).....................................................................74

Fields: Now With 35% Less Typing!.....................................................................................78

Galleries, Give Or Take The Art...........................................................................................82

Employing Fancy Widgets and Containers..................................................................83

Pick and Choose....................................................................................................................83

Time Keeps Flowing Like a River.........................................................................................88

Making Progress....................................................................................................................89

Putting It On My Tab...........................................................................................................90

The Pieces.......................................................................................................................91

The Idiosyncrasies..........................................................................................................91

Wiring It Together........................................................................................................93

Other Containers of Note.....................................................................................................96

Applying Menus............................................................................................................97

Flavors of Menu.....................................................................................................................97

Menus of Options.................................................................................................................98

Menus in Context................................................................................................................100

Taking a Peek.......................................................................................................................102

Embedding the WebKit Browser................................................................................107

A Browser, Writ Small.........................................................................................................107

Loading It Up.......................................................................................................................109

Navigating the Waters..........................................................................................................111

v

Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License EditionEntertaining the Client.........................................................................................................111

Settings, Preferences, and Options (Oh, My!)....................................................................114

Showing Pop-Up Messages..........................................................................................117

Raising Toasts........................................................................................................................117

Alert! Alert!............................................................................................................................118

Checking Them Out.............................................................................................................119

Dealing with Threads..................................................................................................123

Getting Through the Handlers............................................................................................123

Messages.......................................................................................................................124

Runnables.....................................................................................................................127

Running In Place..................................................................................................................127

Utilities (And I Don’t Mean Water Works).......................................................................128

And Now, The Caveats........................................................................................................128

Handling Activity Lifecycle Events..............................................................................131

Schroedinger’s Activity.........................................................................................................131

Life, Death, and Your Activity.............................................................................................132

onCreate() and onCompleteThaw()............................................................................132

onStart(), onRestart(), and onResume().....................................................................133

onPause(), onFreeze(), onStop(), and onDestroy()...................................................134

Using Preferences........................................................................................................137

Getting What You Want......................................................................................................137

Stating Your Preference.......................................................................................................138

A Preference For Action......................................................................................................138

Accessing Files.............................................................................................................143

You And The Horse You Rode In On.................................................................................143

Readin’ ’n Writin’.................................................................................................................147

Working with Resources..............................................................................................151

The Resource Lineup............................................................................................................151

String Theory........................................................................................................................152

Plain Strings..................................................................................................................152

String Formats..............................................................................................................153

Styled Text.....................................................................................................................153

Styled Formats..............................................................................................................154

vi

Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License EditionGot the Picture?...................................................................................................................158

XML: The Resource Way.....................................................................................................160

Miscellaneous Values...........................................................................................................163

Dimensions...................................................................................................................163

Colors............................................................................................................................164

Arrays............................................................................................................................165

Different Strokes for Different Folks..................................................................................166

Managing and Accessing Local Databases...................................................................171

A Quick SQLite Primer........................................................................................................172

Start at the Beginning..........................................................................................................173

Setting the Table..................................................................................................................174

Makin’ Data..........................................................................................................................174

What Goes Around, Comes Around...................................................................................176

Raw Queries..................................................................................................................176

Regular Queries............................................................................................................177

Building with Builders.................................................................................................177

Using Cursors...............................................................................................................179

Change for the Sake of Change...................................................................................179

Making Your Own Cursors..........................................................................................180

Data, Data, Everywhere.......................................................................................................180

Leveraging Java Libraries............................................................................................183

The Outer Limits..................................................................................................................183

Ants and Jars........................................................................................................................184

Communicating via the Internet................................................................................187

REST and Relaxation............................................................................................................187

HTTP Operations via Apache Commons...................................................................188

Parsing Responses........................................................................................................190

Stuff To Consider.........................................................................................................192

Email over Java.....................................................................................................................193

Creating Intent Filters................................................................................................199

What’s Your Intent?............................................................................................................200

Pieces of Intents..........................................................................................................200

Stock Options...............................................................................................................201

vii

Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License EditionIntent Routing.............................................................................................................202

Stating Your Intent(ions)....................................................................................................203

Narrow Receivers.................................................................................................................205

Launching Activities and Sub-Activities.....................................................................207

Peers and Subs.....................................................................................................................208

Start ’Em Up........................................................................................................................208

Make an Intent............................................................................................................209

Make the Call...............................................................................................................209

Finding Available Actions via Introspection...............................................................215

Pick ’Em................................................................................................................................216

Adaptable Adapters.............................................................................................................220

Would You Like to See the Menu?.....................................................................................223

Asking Around.....................................................................................................................225

Using a Content Provider...........................................................................................229

Pieces of Me.........................................................................................................................229

Getting a Handle.................................................................................................................230

Makin’ Queries.....................................................................................................................231

Adapting to the Circumstances..........................................................................................233

Doing It By Hand.................................................................................................................235

Position.........................................................................................................................235

Getting Properties.......................................................................................................236

Setting Properties........................................................................................................237

Give and Take......................................................................................................................238

Beware of the BLOB!...........................................................................................................239

Building a Content Provider.......................................................................................241

First, Some Dissection.........................................................................................................241

Next, Some Typing..............................................................................................................242

Step #1: Create a Provider Class..........................................................................................243

ContentProvider..........................................................................................................243

DatabaseContentProvider...........................................................................................252

Step #2: Supply a Uri...........................................................................................................252

Step #3: Declare the Properties..........................................................................................252

Step #4: Update the Manifest.............................................................................................253

viii

Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License EditionNotify-On-Change Support................................................................................................254

Requesting and Requiring Permissions.....................................................................257

Mother, May I?....................................................................................................................258

Halt! Who Goes There?.......................................................................................................259

Enforcing Permissions via the Manifest....................................................................260

Enforcing Permissions Elsewhere...............................................................................261

May I See Your Documents?...............................................................................................262

Creating a Service........................................................................................................263

Getting Buzzed....................................................................................................................264

Service with Class................................................................................................................264

When IPC Attacks!..............................................................................................................266

Write the AIDL............................................................................................................267

Implement the Interface.............................................................................................268

Manifest Destiny.................................................................................................................270

Where’s the Remote?...........................................................................................................271

Invoking a Service.......................................................................................................273

Bound for Success...............................................................................................................274

Request for Service..............................................................................................................276

Prometheus Unbound.........................................................................................................276

Manual Transmission..........................................................................................................276

Alerting Users Via Notifications.................................................................................279

Types of Pestering...............................................................................................................279

Hardware Notifications..............................................................................................280

Icons..............................................................................................................................281

Letting Your Presence Be Felt.............................................................................................281

Accessing Location-Based Services.............................................................................287

Location Providers: They Know Where You’re Hiding....................................................288

Finding Yourself..................................................................................................................288

On the Move........................................................................................................................292

Are We There Yet? Are We There Yet? Are We There Yet?............................................292

Testing...Testing..................................................................................................................296

Mapping with MapView and MapActivity..................................................................299

The Bare Bones....................................................................................................................299

ix

Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License EditionExercising Your Control.......................................................................................................301

Zoom.............................................................................................................................301

Center...........................................................................................................................302

Reticle...........................................................................................................................303

Traffic and Terrain...............................................................................................................303

Follow You, Follow Me........................................................................................................305

Layers Upon Layers.............................................................................................................307

Overlay Classes............................................................................................................308

Drawing the Overlay...................................................................................................308

Handling Screen Taps..................................................................................................310

Playing Media..............................................................................................................313

Get Your Media On..............................................................................................................314

Making Noise........................................................................................................................315

Moving Pictures....................................................................................................................321

Handling Telephone Calls..........................................................................................325

No, No, No – Not That IPhone...........................................................................................326

What’s Our Status?..............................................................................................................326

You Make the Call!..............................................................................................................326

Searching with SearchManager...................................................................................333

Hunting Season....................................................................................................................333

Search Yourself.....................................................................................................................335

Craft the Search Activity.............................................................................................336

Update the Manifest....................................................................................................340

Try It Out.....................................................................................................................342

The TourIt Sample Application..................................................................................347

Installing TourIt..................................................................................................................347

Demo Location Provider.............................................................................................347

SD Card Image with Sample Tour..............................................................................348

Running TourIt....................................................................................................................349

Main Activity................................................................................................................350

Configuration Activity.................................................................................................352

Cue Sheet Activity.......................................................................................................354

Map Activity.................................................................................................................355

x

Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License EditionTour Update Activity...................................................................................................357

Help Activity................................................................................................................358

TourIt’s Manifest.................................................................................................................359

TourIt’s Content..................................................................................................................360

Data Storage.................................................................................................................361

Content Provider..........................................................................................................361

Model Classes...............................................................................................................361

TourIt’s Activities................................................................................................................362

TourListActivity...........................................................................................................362

TourViewActivity.........................................................................................................363

TourMapActivity..........................................................................................................367

TourEditActivity..........................................................................................................367

HelpActivity.................................................................................................................367

ConfigActivity..............................................................................................................368

 


Direct Download Links:

  • Special Creative Commons BY-NC-SA 3.0 License Edition
  • Download the PDF ebook
  • libros.metabiblio...0-9816780-0-9.pdf
  • Buy this book
  • www.pricestube.co...ok/9780981678009/

Related Books at ISBNlib


    Spread the word

    
    power by isbnlib

    Search


    Popular Books

    Links

    • ISBNLib.com-your online book library
    • OPENISBN-ISBN Search & Book Data Services
    • Digmybook.com-Discover books you like
    • Rapid4Search.com-Rapidshare file search
    • 英文原版书网
    • CCEBook.org-We introduce valuable books
    • Goeway.me-Mechanical Design Blog
    • CCEmagz.com-CCE Magazines, daily free magazines
    • Rsbay.com-The Rapidshare Search
    • iFileSearch.net-Search files inside iFile.it