Beginning Android 2 [Murphy 2010-03-19](1).pdf

(8948 KB) Pobierz
Begin the journey toward your own
successful Android 2 applications
Android 2
Mark L. Murphy
Beginning
Beginning Android 2
■■■
Mark L. Murphy
i
Beginning Android 2
Copyright © 2010 by Mark L. Murphy
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-2629-1
ISBN-13 (electronic): 978-1-4302-2630-7
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 US and other countries. Apress, Inc., is not affiliated with Sun Microsystems, Inc., and
this book was written without endorsement from Sun Microsystems, Inc.
President and Publisher: Paul Manning
Lead Editor: Steve Anglin
Development Editor: Matthew Moodie
Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell,
Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan
Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic
Shakeshaft, Matt Wade, Tom Welsh
Coordinating Editor: Fran Parnell
Copy Editor:
Marilyn S. Smith
Compositor: MacPS, LLC
Indexer: John Collin
Artist: April Milne
Cover Designer: Anna Ishchenko
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
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/info/bulksales
.
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.
ii
Contents at a Glance
■Contents
at a Glance ................................................................................................................................. iv
■Contents.....................................................................................................................................................
v
■About
the Author..................................................................................................................................... xiii
■Acknowledgments
...................................................................................................................................xiv
■Preface......................................................................................................................................................xv
■Chapter
1: The Big Picture ......................................................................................................................... 1
■Chapter
2: Projects and Targets ................................................................................................................ 5
■Chapter
3: Creating a Skeleton Application ............................................................................................. 17
■Chapter
4: Using XML-Based Layouts...................................................................................................... 23
■Chapter
5: Employing Basic Widgets ....................................................................................................... 29
■Chapter
6: Working with Containers........................................................................................................ 39
■Chapter
7: Using Selection Widgets......................................................................................................... 59
■Chapter
8: Getting Fancy with Lists......................................................................................................... 75
■Chapter
9: Employing Fancy Widgets and Containers ............................................................................. 95
■Chapter
10: The Input Method Framework ............................................................................................ 117
■Chapter
11: Applying Menus.................................................................................................................. 125
■Chapter
12: Fonts................................................................................................................................... 137
■Chapter
13: Embedding the WebKit Browser......................................................................................... 141
■Chapter
14: Showing Pop-Up Messages ................................................................................................ 149
■Chapter
15: Dealing with Threads ......................................................................................................... 155
■Chapter
16: Handling Activity Life Cycle Events .................................................................................... 167
■Chapter
17: Creating Intent Filters ........................................................................................................ 171
■Chapter
18: Launching Activities and Subactivities .............................................................................. 177
■Chapter
19: Handling Rotation............................................................................................................... 185
■Chapter
20: Working with Resources .................................................................................................... 197
■Chapter
21: Using Preferences .............................................................................................................. 213
■Chapter
22: Managing and Accessing Local Databases ........................................................................ 225
■Chapter
23: Accessing Files................................................................................................................... 237
■Chapter
24: Leveraging Java Libraries.................................................................................................. 245
■Chapter
25: Communicating via the Internet......................................................................................... 253
■Chapter
26: Using a Content Provider.................................................................................................... 259
■Chapter
27: Building a Content Provider ............................................................................................... 265
■Chapter
28: Requesting and Requiring Permissions ............................................................................. 275
■Chapter
29: Creating a Service .............................................................................................................. 279
■Chapter
30: Invoking a Service.............................................................................................................. 285
■Chapter
31: Alerting Users via Notifications.......................................................................................... 289
■Chapter
32: Accessing Location-Based Services................................................................................... 295
■Chapter
33: Mapping with MapView and MapActivity........................................................................... 301
■Chapter
34: Handling Telephone Calls ................................................................................................... 311
■Chapter
35: Development Tools ............................................................................................................. 315
■Chapter
36: Handling Multiple Screen Sizes.......................................................................................... 331
■Chapter
37: Dealing with Devices .......................................................................................................... 353
■Chapter
38: Handling Platform Changes................................................................................................ 359
■Chapter
39: Where Do We Go from Here? .............................................................................................. 367
■Index
...................................................................................................................................................... 369
iv
Contents
■Contents
at a Glance....................................................................................... iv
■Contents
.......................................................................................................... v
■About
the Author .......................................................................................... xiii
■Acknowledgments.........................................................................................xiv
■Preface
...........................................................................................................xv
■Chapter
1: The Big Picture............................................................................... 1
Challenges of Smartphone Programming ...............................................................................................................1
What Androids Are Made Of....................................................................................................................................2
Stuff at Your Disposal .............................................................................................................................................3
■Chapter
2: Projects and Targets...................................................................... 5
Pieces and Parts .....................................................................................................................................................5
Creating a Project ...................................................................................................................................................6
Project Structure.....................................................................................................................................................6
Root Contents ....................................................................................................................................................6
The Sweat Off Your Brow...................................................................................................................................7
And Now, the Rest of the Story..........................................................................................................................7
What You Get Out of It........................................................................................................................................8
Inside the Manifest .................................................................................................................................................8
In the Beginning, There Was the Root, And It Was Good ...................................................................................9
Permissions, Instrumentations, and Applications (Oh My!) ...............................................................................9
Your Application Does Something, Right? .......................................................................................................10
Achieving the Minimum ...................................................................................................................................11
Version=Control ...............................................................................................................................................12
Emulators and Targets..........................................................................................................................................12
Virtually There..................................................................................................................................................12
Aiming at a Target ...........................................................................................................................................14
v
Zgłoś jeśli naruszono regulamin