Apress Pro Android (2009).pdf

(4571 KB) Pobierz
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Pro Android
Sayed Y. Hashimi and Satya Komatineni
850633231.045.png 850633231.046.png 850633231.047.png 850633231.048.png 850633231.001.png 850633231.002.png 850633231.003.png 850633231.004.png 850633231.005.png 850633231.006.png 850633231.007.png
Pro Android
Copyright © 2009 by Sayed Y. Hashimi and Satya Komatineni
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-1596-7
ISBN-13 (electronic): 978-1-4302-1597-4
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Java and all Java -based marks are trademarks or registered trademarks of Sun Microsystems, Inc., in
the United States and other countries.
Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book was written without endorsement
from Sun Microsystems, Inc.
Lead Editor: Steve Anglin
Development Editor: Douglas Pundick
Technical Reviewer: Vikram Goyal
Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell,
Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper,
Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Project Manager: Richard Dal Porto
Copy Editor: Nina Goldschlager Perry
Associate Production Director: Kari Brooks-Copony
Production Editor: Candace English
Compositor: Patrick Cunningham
Proofreader: Lisa Hamilton
Indexer: Ron Strauss
Artist: April Milne
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 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 http://www.springeronline.com .
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600,
Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com , or visit http://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 http://www.apress.com/info/bulksales .
The information in this book is distributed on an “as is” basis, without warranty. Although every precau-
tion 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 indi-
rectly by the information contained in this work.
The source code for this book is available to readers at http://www.apress.com .
850633231.008.png 850633231.009.png 850633231.010.png 850633231.011.png 850633231.012.png
Contents at a Glance
About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Acknowledgments ................................................................xix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
CHAPter 1
Introducing the Android Computing Platform ......................1
CHAPter 2
Getting Your Feet Wet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
CHAPter 3
Using Resources, Content Providers, and Intents .................43
CHAPter 4
Building User Interfaces and Using Controls . . . . . . . . . . . . . . . . . . . . 107
CHAPter 5
Working with Menus and Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
CHAPter 6
Unveiling 2D Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
CHAPter 7
Exploring Security and Location-Based Services ................225
CHAPter 8
Building and Consuming Services ..............................263
CHAPter 9
Using the Media Framework and Telephony APIs . . . . . . . . . . . . . . . 301
CHAPter 10
Programming 3D Graphics with OpenGL ........................325
CHAPter 11
Managing and Organizing Preferences . . . . . . . . . . . . . . . . . . . . . . . . . 363
CHAPter 12
Coming to Grips with 1.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
CHAPter 13
Simplifying OpenGL and Exploring Live Folders . . . . . . . . . . . . . . . . . 395
INDeX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
v
850633231.013.png 850633231.014.png 850633231.015.png 850633231.016.png 850633231.017.png 850633231.018.png 850633231.019.png 850633231.020.png 850633231.021.png 850633231.022.png 850633231.023.png 850633231.024.png 850633231.025.png 850633231.026.png 850633231.027.png 850633231.028.png 850633231.029.png 850633231.030.png 850633231.031.png
Contents
About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Acknowledgments ................................................................xix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
CHAPter 1
Introducing the Android Computing Platform ..............1
History of Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Delving into the Dalvik VM .........................................4
Comparing Android and Java ME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Understanding the Android Software Stack . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Developing an End-User Application with the Android SDK .............9
The Android Emulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
The Android UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
The Android Foundational Components ........................11
Advanced UI Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Android Service Components .................................13
Android Media and Telephony Components . . . . . . . . . . . . . . . . . . . . 14
Android Java Packages ......................................15
Taking Advantage of Android Source Code ..........................18
Summary .......................................................19
CHAPter 2
Getting Your Feet Wet .......................................21
Setting Up Your Environment ......................................21
Downloading JDK 6 and Eclipse 3.4 ...........................21
Downloading the Android SDK ................................22
Installing Android Development Tools (ADT) ....................22
Learning the Fundamental Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Activity ....................................................24
Intent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Content Provider ............................................25
Service ....................................................25
AndroidManifest.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
vii
850633231.032.png 850633231.033.png 850633231.034.png 850633231.035.png 850633231.036.png 850633231.037.png 850633231.038.png
viii
CONTENTS
Hello World! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Exploring the Structure of an Android Application ....................28
Analyzing the Notepad Application .................................30
Loading and Running the Notepad Application ..................31
Dissecting the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Examining the Application Lifecycle ................................38
Debugging Your App .............................................41
Summary .......................................................42
CHAPter 3
Using resources, Content Providers, and Intents . . . . . . . . 43
Understanding Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
String Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Layout Resources ...........................................45
Resource-Reference Syntax ..................................47
Defining Your Own Resource IDs for Later Use . . . . . . . . . . . . . . . . . . 48
Compiled and Noncompiled Android Resources .................48
Enumerating Key Android Resources ..........................49
Working with Arbitrary XML Resource Files . . . . . . . . . . . . . . . . . . . . 57
Working with Raw Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Working with Assets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Reviewing the Resources Directory Structure ...................60
Understanding Content Providers ..................................60
Exploring Android’s Built-in Providers . . . . . . . . . . . . . . . . . . . . . . . . . 61
Architecture of Content Providers .............................67
Implementing Content Providers ..............................79
Understanding Intents ............................................91
Available Intents in Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Intents and Data URIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Generic Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Using Extra Information ......................................96
Using Components to Directly Invoke an Activity ................97
Best Practice for Component Designers . . . . . . . . . . . . . . . . . . . . . . . 99
Understanding Intent Categories ..............................99
The Rules for Resolving Intents to Their Components ...........102
Exercising the ACTION_PICK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Exercising the GET_CONTENT Action .........................104
Summary ......................................................106
850633231.039.png 850633231.040.png 850633231.041.png 850633231.042.png 850633231.043.png 850633231.044.png
Zgłoś jeśli naruszono regulamin