logo资料库

Android Application Development.pdf

第1页 / 共337页
第2页 / 共337页
第3页 / 共337页
第4页 / 共337页
第5页 / 共337页
第6页 / 共337页
第7页 / 共337页
第8页 / 共337页
资料共337页,剩余部分请下载后查看
Table of Contents
Preface
Audience
How This Book Is Organized
Conventions Used in This Book
Using Code Examples
Safari® Books Online
How to Contact Us
Acknowledgments
Rick Rogers
John Lombardo
Zigurd Mednieks
Blake Meike
Part I. Development Kit Walk-Through
Chapter 1. Getting to Know Android
Why Android?
The Open Handset Alliance
The Android Execution Environment
Components of an Android Application
Android Activity Lifecycle
Android Service Lifecycle
How This Book Fits Together
Chapter 2. Setting Up Your Android Development Environment
Setting Up Your Development Environment
Creating an Android Development Environment
Hello, Android
Where We’re Going
Starting a New Android Application: HelloWorld
Writing HelloWorld
Running HelloWorld
Chapter 3. Using the Android Development Environment for Real Applications
MicroJobs: This Book’s Main Sample Application
Android and Social Networking
Downloading the MJAndroid Code
A Brief Tour of the MJAndroid Code
The Project Root Folder (MJAndroid)
The Source Folder (src)
The Resource Folder (res)
First Steps: Building and Running the MicroJobs Application
A Very Short Tour of the Android SDK/Eclipse IDE
Loading and Starting the Application
Digging a Little Deeper: What Can Go Wrong?
Running an Application on the T-Mobile Phone
Enable USB debugging on your phone
Load the USB driver for ADB
Connecting the phone
Running MicroJobs on the phone
Summary
Chapter 4. Under the Covers: Startup Code and Resources in the MJAndroid Application
Initialization Parameters in AndroidManifest.xml
Initialization in MicroJobs.java
More Initialization of MicroJobs.java
Summary
Chapter 5. Debugging Android Applications
The Tools
Eclipse Java Editor
Java Errors
The Debugger
Logcat
Looking at logcat to solve runtime errors
Writing your own logcat entries
Android Debug Bridge (adb)
DDMS: Dalvik Debug Monitor Service
Traceview
Trace data collection
Summary
Chapter 6. The ApiDemos Application
Application Setup in the Manifest File
Finding the Source to an Interesting Example
Custom Title Demo
Linkify Demo
Adding Your Own Examples to ApiDemos
Chapter 7. Signing and Publishing Your Application
Test Your Application
Attach an End User License Agreement If Desired
Create and Attach an Icon and Label
Clean Up for Release
Version Your Application
Obtaining a Signing Certificate and API Key
Getting a Signing Certificate for an Application You Are Going to Ship
Generating a key pair (public and private keys) and a signing certificate
Getting a Signing Certificate While Debugging
Getting the MD5 fingerprint of your Debug signing certificate
Getting a Map API Key from Google
Signing Your Application
Retesting Your Application
Publishing on Android Market
Signing Up As an Android Developer
Uploading Your Application
Part II. Programming Topics
Chapter 8. Persistent Data Storage: SQLite Databases and Content Providers
Databases
Basic Structure of the MicroJobsDatabase Class
Reading Data from the Database
Modifying the Database
Inserting data into the database
Updating data already in the database
Deleting data in the database
Content Providers
Introducing NotePad
Activities
Database
Structure of the source code
Content Providers
Implementing a content provider
Consuming a Content Provider
Create data (insert)
Read/query data
Update data
Delete data
Chapter 9. Location and Mapping
Location-Based Services
Mapping
The Google Maps Activity
The MapView and MapActivity
Working with MapViews
MapView and MyLocationOverlay Initialization
Pausing and Resuming a MapActivity
Controlling the Map with Menu Buttons
Controlling the Map with the KeyPad
Location Without Maps
The Manifest and Layout Files
Connecting to a Location Provider and Getting Location Updates
Updating the Emulated Location
Using geo to update location
Using DDMS to update location
Chapter 10. Building a View
Android GUI Architecture
The Model
The View
The Controller
Putting It Together
Assembling a Graphical Interface
Wiring Up the Controller
Listening to the Model
Listening for Touch Events
Listening for Key Events
Alternative Ways to Handle Events
Advanced Wiring: Focus and Threading
The Menu
Chapter 11. A Widget Bestiary
Android Views
TextView and EditText
Button and ImageButton
Adapters and AdapterViews
CheckBoxes, RadioButtons, and Spinners
ViewGroups
Gallery and GridView
ListView and ListActivity
ScrollView
TabHost
Layouts
Frame Layout
LinearLayout
TableLayout
AbsoluteLayout
RelativeLayout
Chapter 12. Drawing 2D and 3D Graphics
Rolling Your Own Widgets
Layout
Measurement
Arrangement
Canvas Drawing
Drawing text
Matrix transformations
Drawables
Bitmaps
Bling
Shadows, Gradients, and Filters
Animation
Transition animation
Background animation
Surface view animation
OpenGL Graphics
Chapter 13. Inter-Process Communication
Intents: Simple, Low-Overhead IPC
Intent Objects Used in Inter-Process Communication
Activity Objects and Navigating the User Interface Hierarchy
Example: An Intent to Pick How We Say “Hello World”
Getting a Result via Inter-Process Communication
Remote Methods and AIDL
Android Interface Definition Language
Classes Underlying AIDL-Generated Interfaces
Implementing the Stub interface
Getting an instance of the remote Proxy object
Publishing an Interface
Android IPC Compared with Java Native Interface (JNI)
What Binder Doesn’t Do
Binder and Linux
Chapter 14. Simple Phone Calls
Quick and Easy Phone Calls
Creating an Example Application to Run the call Method
Embedding the Code Snippet in a Simple Application
Exploring the Phone Code Through the Debugger
Creating an Instance of an Intent
Adding Data to an Instance of an Intent
Initiating a Phone Call
Exception Handling
Android Application-Level Modularity and Telephony
Chapter 15. Telephony State Information and Android Telephony Classes
Operations Offered by the android.telephony Package
Package Summary
Limitations on What Applications Can Do with the Phone
Example: Determining the State of a Call
Android Telephony Internals
Inter-Process Communication and AIDL in the android.internal.telephony Package
The android.internal.telephony Package
The android.internal.telephony.gsm Package
Exploring Android Telephony Internals
Android and VoIP
Appendix. Wireless Protocols
Prehistory
The Dawn of Second Generation (2G) Digital Cellular
Improved Digital Cellular (2.5G)
The Rise of 3G
The Future: 4G
Index
Android Application Development Rick Rogers, John Lombardo, Zigurd Mednieks, and Blake Meike Beijing•Cambridge•Farnham•Köln•Sebastopol•Taipei•Tokyo
Android Application Development by Rick Rogers, John Lombardo, Zigurd Mednieks, and Blake Meike Copyright © 2009 Rick Rogers, John Lombardo, Zigurd Mednieks, and Blake Meike. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://mysafaribooksonline.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com. Editor: Andy Oram Production Editor: Sumita Mukherji Copyeditor: Genevieve d’Entremont Proofreader: Sada Preisch Printing History: May 2009: First Edition. Joe Wizda Indexer: Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Android Application Development, the image of an Eastern quoll and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. This book uses RepKover™, a durable and flexible lay-flat binding. ISBN: 978-0-596-52147-9 [M] 1241533714 TM
Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix Part I. Development Kit Walk-Through 1. Getting to Know Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 3 4 5 6 8 10 10 Why Android? The Open Handset Alliance The Android Execution Environment Components of an Android Application Android Activity Lifecycle Android Service Lifecycle How This Book Fits Together Setting Up Your Development Environment Hello, Android Creating an Android Development Environment 2. Setting Up Your Android Development Environment . . . . . . . . . . . . . . . . . . . . . . . . . 13 13 14 18 18 18 22 24 Where We’re Going Starting a New Android Application: HelloWorld Writing HelloWorld Running HelloWorld MicroJobs: This Book’s Main Sample Application Android and Social Networking Downloading the MJAndroid Code A Brief Tour of the MJAndroid Code 3. Using the Android Development Environment for Real Applications . . . . . . . . . . . . 27 27 27 30 30 30 31 32 33 The Project Root Folder (MJAndroid) The Source Folder (src) The Resource Folder (res) First Steps: Building and Running the MicroJobs Application iii
A Very Short Tour of the Android SDK/Eclipse IDE Loading and Starting the Application Digging a Little Deeper: What Can Go Wrong? Running an Application on the T-Mobile Phone Summary 33 35 36 39 41 Initialization Parameters in AndroidManifest.xml Initialization in MicroJobs.java 4. Under the Covers: Startup Code and Resources in the MJAndroid Application . . . . 43 44 46 52 56 More Initialization of MicroJobs.java Summary The Tools Eclipse Java Editor 5. Debugging Android Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 57 58 58 64 67 71 74 75 80 Java Errors The Debugger Logcat Android Debug Bridge (adb) DDMS: Dalvik Debug Monitor Service Traceview Summary Application Setup in the Manifest File Finding the Source to an Interesting Example 6. The ApiDemos Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 81 83 83 84 84 Adding Your Own Examples to ApiDemos Custom Title Demo Linkify Demo Test Your Application Attach an End User License Agreement If Desired Create and Attach an Icon and Label Clean Up for Release Version Your Application Obtaining a Signing Certificate and API Key 7. Signing and Publishing Your Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 88 89 89 90 90 90 91 93 95 96 96 96 Getting a Signing Certificate for an Application You Are Going to Ship Getting a Signing Certificate While Debugging Signing Your Application Retesting Your Application Publishing on Android Market Signing Up As an Android Developer iv | Table of Contents
Uploading Your Application 96 Part II. Programming Topics Databases Basic Structure of the MicroJobsDatabase Class Reading Data from the Database Modifying the Database 8. Persistent Data Storage: SQLite Databases and Content Providers . . . . . . . . . . . . . 101 101 102 107 110 114 116 118 129 Introducing NotePad Content Providers Consuming a Content Provider Content Providers 9. Location and Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 137 Location-Based Services 139 Mapping 139 The Google Maps Activity 140 The MapView and MapActivity Working with MapViews 140 141 144 145 147 148 148 149 152 The Manifest and Layout Files Connecting to a Location Provider and Getting Location Updates Updating the Emulated Location MapView and MyLocationOverlay Initialization Pausing and Resuming a MapActivity Controlling the Map with Menu Buttons Controlling the Map with the KeyPad Location Without Maps Android GUI Architecture The Model The View The Controller Putting It Together 10. Building a View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 157 157 158 159 159 161 166 168 173 176 177 179 Assembling a Graphical Interface Wiring Up the Controller Listening to the Model Listening for Touch Events Listening for Key Events Alternative Ways to Handle Events Advanced Wiring: Focus and Threading Table of Contents | v
The Menu 183 ViewGroups Android Views TextView and EditText Button and ImageButton Adapters and AdapterViews CheckBoxes, RadioButtons, and Spinners 11. A Widget Bestiary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 188 188 191 192 193 198 198 202 204 205 208 209 209 213 215 216 Gallery and GridView ListView and ListActivity ScrollView TabHost Frame Layout LinearLayout TableLayout AbsoluteLayout RelativeLayout Layouts Rolling Your Own Widgets Layout Canvas Drawing Drawables Bitmaps 12. Drawing 2D and 3D Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 221 222 226 237 242 243 246 247 252 Shadows, Gradients, and Filters Animation OpenGL Graphics Bling 13. Intent Objects Used in Inter-Process Communication Activity Objects and Navigating the User Interface Hierarchy Example: An Intent to Pick How We Say “Hello World” Getting a Result via Inter-Process Communication Inter-Process Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 258 Intents: Simple, Low-Overhead IPC 258 259 259 262 265 266 270 273 274 275 Android Interface Definition Language Classes Underlying AIDL-Generated Interfaces Publishing an Interface Android IPC Compared with Java Native Interface (JNI) What Binder Doesn’t Do Remote Methods and AIDL vi | Table of Contents
分享到:
收藏