Tải bản đầy đủ

Beginning Android Tablet Programming potx


Beginning Android Tablet
Starting with Android Honeycomb for Tablets

  
Robbie Matthews

Beginning Android Tablet Programming: Starting with Android Honeycomb for Tablets

Copyright © 2011 by Robbie Matthews
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-4302-3783-9
ISBN-13 (electronic): 978-1-4302-3784-6
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol
with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only
in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of
the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are
not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject
to proprietary rights.
President and Publisher: Paul Manning
Lead Editor: Steve Anglin and Tom Welsh
Technical Reviewer: Stephen Bull and Peter Brownlow
Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Morgan Engel,
Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham,
Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic
Shakeshaft, Gwenan Spearing, Matt Wade, Tom Welsh
Coordinating Editor: Anita Castro
Copy Editor: Mary Ann Fugate
Compositor: Bytheway Publishing Services
Indexer: SPI Global
Artist: SPI Global
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street,
6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-
sbm.com, or visit www.springeronline.com.
For information on translations, please e-mail rights@apress.com, or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.
eBook versions and licenses are also available for most titles. For more information, reference our
Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.
The information in this book is distributed on an “as is” basis, without warranty. Although every
precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have
any liability to any person or entity with respect to any loss or damage caused or alleged to be caused
directly or indirectly by the information contained in this work.
The source code for this book is available to readers at www.apress.com. You will need to answer
questions pertaining to this book in order to successfully download the code.

I dedicate this book to my family. Without your support and occasional nagging
this book would never have been finished.
Robbie Matthews

Contents at a Glance
 About the Author xiv

About the Technical Reviewer xv
 Acknowledgments xvi
 Some Notes on Using the Downloaded Code xvii
 Chapter 1: Getting Started 1

Chapter 2: How Android Works 27

Chapter 3: What Can You Do with an Android Tablet? 53
 Chapter 4: Beyond Java: Programming in Python and Friends 79
 Chapter 5: Project 1: Media Player 105

Chapter 6: Explorer 123
 Chapter 7: Contact Manager 151
 Chapter 8: Dude, Where’s My Car? 175

Chapter 10: Remind Me 221
 Chapter 11: Everything Else 243
 Index 269
 About the Author xiv

About the Technical Reviewer xv
 Acknowledgments xvi

Some Notes on Using the Downloaded Code xvii
 Chapter 1: Getting Started 1
A Short, Personal History of Portable Programming 1
The Advent of Android 3
Preparing Your Computer 4
Installing Your Development Environment 4
Creating an Emulator Instance 5
Setting Your Path 6
Your First Android Program 6
Oh, No! Java! 9
A Quick Guide to Java 10
Structure 11
Primitives 12
Flow Control 12
Objects 13
Constructors, Initializers, and Overloading 17
Where’s “Free”? 19
String Handling 20
Packages 21
Lists and Maps 22
Generics 23
Inheritance and Interfaces 24
Annotations 25
And Many More 26
Summary 26

Chapter 2: How Android Works 27
Basic Structure of Android Programs 27
Lifecycle 38
Intents: What, Where, Why, and Are They Honorable? 42
Intent Filters 47
Common Intents 48
Finally 48
Secretly Linux 49
Summary 51

Chapter 3: What Can You Do with an Android Tablet? 53
More Sensors Than the CIA 54
Lights, Camera, Action 63
Browsing for Fun and Profit 64
Managing Your Assets 66
Getting Fancy 67
My Little Black Book—Managing Contacts 69
Accessing Contacts 69
A Quick Side Trip into Lists 72
Different Things to Access 74
Share My Stuff (Sending and Receiving, Well, Everything) 75
Bonus Stuff—Menu Options 75
Sending E-mail 76
Sound and Fury (Managing Media Files) 77
Summary 78

Chapter 4: Beyond Java: Programming in Python and Friends 79
Why Use Another Language? 80
Getting Started 80
Script Management 80
Help! I Need Somebody 82
Python Help 82
What’s a Facade? 82
Intents (Again) 83
Different Ways of Using the Intent Methods 84
User Interaction 86
Events 89
Media Files 91
Controlling Your Phone 94
Where Am I? 95
Battery 97
Keeping the Device Awake 99
Editing Tips 100
Contacts and Phone Numbers 102
SQL 103
More Stuff 104
Summary 104
 Chapter 5: Project 1: Media Player 105
Fragments 105
What’s a Fragment? 105
Examining the Example 106
The ActionBar 109
FragmentManager 111
Media Player Application 112
Displaying a List from a Cursor 114
Querying Media Files 115
Responding to Clicks 115
Some More on the ActionBar 115
Menu 116
Getting a Media URI 116
Playing Media 117
Different Media Sources 118
Dialog Boxes 118
Changing a List Cursor 120
Advanced Media Playing 120
Displaying Our Progress 121
Controlling Our Progress 122
Summary 122

Chapter 6: Explorer 123
Anatomy of a File Explorer 123
ListActivity for Fun and Profit 124
A Note on Resources 125
Build Your Own List Adapter 126
Inflation 128
Populating the List 128
Finding Out About Your File 130
Learning to Mime 133
Dating Your File 133
Going Native 133
State Your Preference 137
Reading Your Preferences 139
Sorting Techniques 141
The Actual Activity 141
Reacting to a List Selection 142
Creation at Last 143
A Different Menu 144
Reacting to a Context Menu 144
Dialogs 145
A Few Last Bits 148
Making It Better 149
Summary 149

Chapter 7: Contact Manager 151
And Now for Something Completely Different 152
The Application 152
Anatomy of a Signpost 152
Knowing When the View Is Available 156
Room for Improvement 156
Backward Compatibility 157
List Handling 158
Saving the List 158
Reusing Your Libraries 159
Import and Export 159
A Last Note on Dialogs 160
Managing Contacts 161
Loaders 162
Starting a Loader 163
How a Loader Works 163
Why a Loader? 163
The Search Bar 164
Contact Details 166
Editing Data 169
Adding a Phone Number 170
Changing Types 171
And Deleting 172
Summary 173
 Chapter 8: Dude, Where’s My Car? 175
Location Manager 175
Location Providers 176
BAT3PGps 176
A Class to Handle the Location Manager 177
Location 181
The Application 183
The Compass 183
Bearing and Distance 185
Are We There Yet? 186
Wakeup Calls 186
Services 187
Notifications 188
Proximity Alerts 190
Proximity Alert Pros and Cons 192
Lists of Targets 193
Where Was That Again? 197
Where to Go from Here? 198
Summary 198

Chapter 9: Let the Games Begin! 199
Threads 199
Java Threads 200
Android Threads 203
Floater 203
SurfaceView 204
Using Your SurfaceHolder 207
Handlers 207
Using the Handler 208
Running the Game 208
Handling Input 208
Custom Fonts 209
So What’s an Asset? 210
Font Example 212
Menus: A Different Approach 212
Into the Third Dimension! 214
It’s a Box 214
More Interprocess Communication Options 219
Summary 220

Chapter 10: Remind Me 221
Alarms 221
Pending Intents 222
Stopping an Alarm 222
Matching PendingIntent 222
Sending SMS 223
Receiving SMS 224
Text To Speech 225
Queues 227
SQLite 227
Creating a SQLite Database 228
Getting Your Database 229
Using Your Database 229
The Application 230
Using the Application 233
Contacts 233
Set Alarm 234
Alarms List 237
Testing TTS 240
Enhancing the Application 241
Summary 241
 Chapter 11: Everything Else 243
Timers 243
Stopping Gracefully 246
Doing Things in Background 247
Asking for Directions 251
The Download Manager 254
Querying Your Downloads 257
Animations 259
Defining an Animation 260
Interpolating . . . in Public, No Less 261
Loading Your Animation 262
USB 262
Programming Your USB 263
Responding to USB Events 265
Summary 267
And Finally 267
 Index 269
About the Author

 Robbie has been a programmer since the early 1980s, when a friend
came home with a Commodore PET. He’s been working with computers
ever since, usually doing “the tricky bits.”
He has worked on just about every aspect of computer
programming you can think of. At one stage, he was the world expert on a
computer language called Sybol, which no one remembers anymore, and
wrote the firmware for some early GPS tracking devices.
His involvement in Android programming started when he began
contributing to the Scripting Layer for Android (SL4A) project, which he is
now maintaining. He’s also an author and editor: he is the editor-in-chief
for the Andromeda Spaceways Inflight Magazine, and in 2003 he won the
Peter McNamara award for Publishing and Editing. In 2009, his collection
of short stories, Johnny Phillips: Werewolf Detective, was shortlisted for an
Aurealis Award.
He is married, has two adult children, and runs his household for the convenience of two small
dogs (or so they inform him). In what spare time he has, he rides his bicycle, plays guitar, and likes role-
playing games.
About the Technical Reviewer

 Steve Bull is a mixed-media technology artist and entrepreneur whose
practice includes extensive software engineering experience. For the last ten
years, he has created location-specific narratives and games that explore the
social, technological, and creative possibilities of cell phones. He codes in
Android, iOS, Asterisk, and most recently in Junaio 3D augmented reality. He’s
recently been commissioned to deliver 50 AR busts into a private Italian
garden bordering the Mediterranean.

I’d like to thank the SL4A crew: I never would have gotten into this otherwise . . . I’d particularly like to
thank Damon Kohler for starting SL4A in the first place: many of the techniques in this book were
cribbed off his foundations. And I’d even like to thank Paul Ferrill, for dobbing me into Apress—without
that, I may have had my weekends free!

Some Notes on Using the
Downloaded Code
There is a significant amount of sample code in this book. The code is downloadable from the Apress
web site: there will be a link from this book’s individual page.
Navigate to the Apress web site, and look for “Beginning Android Tablet Programming.” I would
expect it to be here: www.apress.com/9781430237839, but links are subject to change. There will be a link
to download the source code, and it will be in a single zip file, probably BA3TPSource.zip.
Setting Up Eclipse
All the coding examples (except those in Python) are written with Eclipse in mind. Chapter 1 will walk
you through setting up your Eclipse environment. Go read it now and follow the instructions. I’ll wait.
Importing the Example Projects
To import the sample projects, open Eclipse and click the following:
File Ê Import Ê General Ê Existing Projects Into Workspace Ê Next
Check “Select Archive File”, and browse to wherever you’ve put BA3TPSource.zip.
A list of available projects should appear. You can either select the projects you are interested
in, or just import all of them. Hit Finish, and let it do its stuff.
The Utilities Library
You may need to do some additional setup for the BA3TPUtils library. This is a set of utility functions
that a number of the later projects share. It needs to know where to find the Android development files.
To set this up, right-click BA3TPUtils, and then go to the following:
Build Path Ê Add External Archive
Navigate to the following:
<wherever you installed the android-sdk>/platforms/<your preferred platform>/android.jar
For example, on my Windows computer, that is as follows:
C:\Program Files (x86)\Android\android-sdk\platforms\android-11\android.jar
Another problem you may find is BA3TPGps or BA3TPContacts2 complaining about either a
missing BA3TPUtils or perhaps it can’t find FileUtils, ListPicker, or MessageBox. This may mean you
neglected to import BA3TPUtils in the first place, or that they can’t find it.
The solution is to import BA3TPUtils according to the following instructions, and (if needed)
tell the complaining projects where to find it.
Right-click the offending project, and go to the following:
Build Path Ê Configure Build Path Ê Projects Ê Add Ê BA3TPUtils Ê OK
Then everything should work.
Python Examples
To save you typing, all the Python example files in Chapter 4 are included in the download. Look for the
Python folder in BA3TPSource.zip. The file examples.txt provides a table of which listing corresponds to
which source file.


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay