Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Pro Android pdf
PREMIUM
Số trang
466
Kích thước
12.0 MB
Định dạng
PDF
Lượt xem
1419

Pro Android pdf

Nội dung xem thử

Mô tả chi tiết

this print for content only—size & color not accurate spine = 0.889" 464 page count

Books for professionals by professionals®

Pro Android

Dear Reader,

Android, Google’s open source platform for mobile development, is destined to

become one of the leading mobile platforms of the future. Demand for develop￾ers who can build software for this platform is increasing rapidly because more

and more organizations are targeting customers who own handheld devices.

Plus, companies want their core applications built on an open platform that is

supported by the Open Handset Alliance.

Pro Android covers mobile application development for the Android

Platform, starting with a history of Android and an overview of the platform’s

main pillars. Its content ranges from basic concepts such as Android resources,

intents, and content providers to mind-wrenching concepts such as OpenGL

3D graphics. We take a pragmatic approach to even the most difficult aspects of

Android programming, and we give you confidence in areas that other authors

avoid. We teach you how to build Android applications by taking you through

the Android APIs one step at a time. We cover the Android SDK from UI devel￾opment, animations, media framework, background services, up through

Android 1.5’s Live Folders.

We wrote this book because we saw the captivation and awe on our custom￾ers’ faces when we presented our Android-based applications. After you apply

the skills you learn in this book, you too will impress your customers with your

sophisticated Android programs.

Regards,

Sayed Y. Hashimi and Satya Komatineni

US $44.99

Shelve in

Mobile Computing

User level:

Intermediate–Advanced

Hashimi,

Komatineni Pro Android

The EXPERT’s VOIce® in Open Source

Pro

Android

 CYAN

MAGENTA

 YELLOW

BLACK

 PANTONE 123 C

Sayed Y. Hashimi and Satya Komatineni

Companion

eBook Available

THE APRESS ROADMAP

Beginning Android

Pro Android

Beginning

Java™ ME Platform

www.apress.com

SOURCE CODE ONLINE

Companion eBook

See last page for details

on $10 eBook version

Covers Google’s Android Platform and its fundamental

APIs, from basic concepts such as Android resources,

intents, and content providers to advanced topics such

as OpenGL 3D graphics

ISBN 978-1-4302-1596-7

9 781430 215967

5 4 4 9 9

Sayed Y. Hashimi,

author of

Deploying .NET Applications:

Learning MSBuild and

ClickOnce

Pro Service-Oriented Smart

Clients with .NET 2.0

Satya Komatineni

Includes

Android 1.5

Pro Android

Sayed Y. Hashimi and Satya Komatineni

15967fm.indd 1 6/5/09 11:14:50 AM

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 [email protected], 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 [email protected], 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.

15967fm.indd 2 6/5/09 11:14:50 AM

To my son: Sayed-Adieb

—Sayed Y. Hashimi

To my family: Narayan Komatineni, Kavitha Komatineni, Nikolas Perez,

Ashley Perez, and AnnMarie Komatineni

—Satya Komatineni

15967fm.indd 3 6/5/09 11:14:50 AM

15967fm.indd 4 6/5/09 11:14:50 AM

v

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

15967fm.indd 5 6/5/09 11:14:50 AM

15967fm.indd 6 6/5/09 11:14:50 AM

vii

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

15967fm.indd 7 6/5/09 11:14:50 AM

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

15967fm.indd 8 6/5/09 11:14:50 AM

■CONTENTS ix

Chapter 4 Building User Interfaces and Using Controls. . . . . . . . . . . . . 107

UI Development in Android. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

Understanding Android’s Common Controls. . . . . . . . . . . . . . . . . . . . . . . . 113

Text Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

Button Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

List Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122

Grid Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

Date and Time Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128

Other Interesting Controls in Android. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130

The MapView Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130

The Gallery Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130

Understanding Layout Managers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131

The LinearLayout Layout Manager. . . . . . . . . . . . . . . . . . . . . . . . . . . 131

The TableLayout Layout Manager. . . . . . . . . . . . . . . . . . . . . . . . . . . . 134

The RelativeLayout Layout Manager. . . . . . . . . . . . . . . . . . . . . . . . . . 139

The AbsoluteLayout Layout Manager. . . . . . . . . . . . . . . . . . . . . . . . . 141

The FrameLayout Layout Manager. . . . . . . . . . . . . . . . . . . . . . . . . . . 143

Customizing Layout for Various Screen Configurations. . . . . . . . . . 145

Understanding Adapters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

Getting to Know SimpleCursorAdapter. . . . . . . . . . . . . . . . . . . . . . . . 146

Getting to Know ArrayAdapter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

Creating Custom Adapters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

Debugging and Optimizing Layouts with the Hierarchy Viewer. . . . . . . . 149

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150

Chapter 5 Working with Menus and Dialogs. . . . . . . . . . . . . . . . . . . . . . . . . 151

Understanding Android Menus. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

Creating a Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153

Responding to Menu Items. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154

Creating a Test Harness for Testing Menus. . . . . . . . . . . . . . . . . . . 156

Working with Other Menu Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

Expanded Menus. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

Working with Icon Menus. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

Working with Submenus. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

Provisioning for System Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

Working with Context Menus. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

Working with Alternative Menus. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

Working with Menus in Response to Changing Data. . . . . . . . . . . . 172

15967fm.indd 9 6/5/09 11:14:50 AM

x ■CONTENTS

Loading Menus Through XML Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172

Structure of an XML Menu Resource File. . . . . . . . . . . . . . . . . . . . . 172

Inflating XML Menu Resource Files. . . . . . . . . . . . . . . . . . . . . . . . . . . 173

Responding to XML-Based Menu Items. . . . . . . . . . . . . . . . . . . . . . . 174

A Brief Introduction to Additional XML Menu Tags. . . . . . . . . . . . . . 175

Using Dialogs in Android. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176

Designing an Alert Dialog. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177

Designing a Prompt Dialog. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179

Nature of Dialogs in Android. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184

Rearchitecting the Prompt Dialog. . . . . . . . . . . . . . . . . . . . . . . . . . . . 185

Working with Managed Dialogs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186

Understanding the Managed-Dialog Protocol. . . . . . . . . . . . . . . . . . 186

Recasting the Nonmanaged Dialog as a Managed Dialog. . . . . . . 186

Simplifying the Managed-Dialog Protocol. . . . . . . . . . . . . . . . . . . . . 188

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196

Chapter 6 Unveiling 2D Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197

Frame-by-Frame Animation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198

Planning for Frame-by-Frame Animation. . . . . . . . . . . . . . . . . . . . . 198

Creating the Activity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199

Adding Animation to the Activity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201

Layout Animation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204

Basic Tweening Animation Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . 204

Planning the Layout-Animation Test Harness. . . . . . . . . . . . . . . . . . 205

Creating the Activity and the ListView. . . . . . . . . . . . . . . . . . . . . . . . 206

Animating the ListView. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209

Using Interpolators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212

View Animation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213

Understanding View Animation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214

Adding Animation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216

Using Camera to Provide Depth Perception in 2D. . . . . . . . . . . . . . 220

Exploring the AnimationListener Class. . . . . . . . . . . . . . . . . . . . . . . . 221

Some Notes on Transformation Matrices. . . . . . . . . . . . . . . . . . . . . 222

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

Chapter 7 Exploring Security and Location-Based Services. . . . . . . . 225

Understanding the Android Security Model. . . . . . . . . . . . . . . . . . . . . . . . . 225

Overview of Security Concepts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

Signing Applications for Deployment. . . . . . . . . . . . . . . . . . . . . . . . . 226

15967fm.indd 10 6/5/09 11:14:50 AM

■CONTENTS xi

Performing Runtime Security Checks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229

Understanding Security at the Process Boundary. . . . . . . . . . . . . . 229

Declaring and Using Permissions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 230

Understanding and Using Custom Permissions. . . . . . . . . . . . . . . . 232

Working with Location-Based Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . 238

Understanding the Mapping Package. . . . . . . . . . . . . . . . . . . . . . . . . 238

Understanding the Location Package. . . . . . . . . . . . . . . . . . . . . . . . . 249

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262

Chapter 8 Building and Consuming Services. . . . . . . . . . . . . . . . . . . . . . . . 263

Consuming HTTP Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263

Using the HttpClient for HTTP GET Requests. . . . . . . . . . . . . . . . . . . 264

Using the HttpClient for HTTP POST Requests. . . . . . . . . . . . . . . . . 266

Dealing with Exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269

Addressing Multithreading Issues. . . . . . . . . . . . . . . . . . . . . . . . . . . . 272

Doing Interprocess Communication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276

Creating a Simple Service. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276

Understanding Services in Android. . . . . . . . . . . . . . . . . . . . . . . . . . . 277

Understanding Local Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278

Understanding AIDL Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282

Defining a Service Interface in AIDL. . . . . . . . . . . . . . . . . . . . . . . . . . 283

Implementing an AIDL Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286

Calling the Service from a Client Application. . . . . . . . . . . . . . . . . . 288

Passing Complex Types to Services. . . . . . . . . . . . . . . . . . . . . . . . . . 292

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300

Chapter 9 Using the Media Framework and Telephony APIs. . . . . . . 301

Using the Media APIs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301

Understanding the setDataSource Method. . . . . . . . . . . . . . . . . . . . 305

Playing Video Content. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307

Understanding the MediaPlayer Oddities. . . . . . . . . . . . . . . . . . . . . . 311

Exploring Audio Recording. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311

Using the Telephony APIs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316

Working with SMS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316

Working with the Telephony Manager. . . . . . . . . . . . . . . . . . . . . . . . 323

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324

15967fm.indd 11 6/5/09 11:14:50 AM

xii ■CONTENTS

Chapter 10 Programming 3D Graphics with OpenGL. . . . . . . . . . . . . . . . . 325

Understanding OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326

OpenGL ES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327

OpenGL ES and Java ME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327

M3G: Another Java ME 3D Graphics Standard. . . . . . . . . . . . . . . . . 328

Using OpenGL ES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328

Essential Drawing with OpenGL ES. . . . . . . . . . . . . . . . . . . . . . . . . . . 329

Understanding the Camera and Coordinates . . . . . . . . . . . . . . . . . . 334

Interfacing OpenGL ES with Android. . . . . . . . . . . . . . . . . . . . . . . . . . 338

Creating and Using the OpenGL Test Harness. . . . . . . . . . . . . . . . . . . . . . 342

Designing the Test Harness. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343

OpenGLTestHarnessActivity.java. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345

OpenGLTestHarness.java. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346

OpenGLDrawingThread.java. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348

EglHelper.java. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352

Renderer.java. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354

AbstractRenderer.java. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354

SimpleTriangleRenderer.java. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355

Changing Camera Settings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358

Using Indices to Add Another Triangle . . . . . . . . . . . . . . . . . . . . . . . 360

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362

Chapter 11 Managing and Organizing Preferences. . . . . . . . . . . . . . . . . . . 363

Exploring the Preferences Framework. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363

Understanding CheckBoxPreference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367

Understanding EditTextPreference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370

Understanding RingtonePreference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371

Organizing Preferences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376

15967fm.indd 12 6/5/09 11:14:50 AM

■CONTENTS xiii

Chapter 12 Coming to Grips with 1.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377

Installing the ADT Plug-in for Android 1.5 Development. . . . . . . . . . . . . 377

Getting Started with Android 1.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379

Creating an Android Virtual Device. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383

Exploring Improvements to the Media Framework. . . . . . . . . . . . . . . . . . 384

Using the MediaRecorder Class for Video Capture. . . . . . . . . . . . . . 385

Exploring the MediaStore Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386

Scanning the Media Store for Media Content. . . . . . . . . . . . . . . . . . 390

Exploring Voice Recognition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392

Introducing the Input-Method Framework. . . . . . . . . . . . . . . . . . . . . . . . . . 394

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394

Chapter 13 Simplifying OpenGL and Exploring Live Folders. . . . . . . . . 395

Simplifying OpenGL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396

Reimplementing the Simple Triangle OpenGL Drawing. . . . . . . . . 398

OpenGL Animation Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401

Exploring Live Folders. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404

How a User Experiences Live Folders. . . . . . . . . . . . . . . . . . . . . . . . . 405

Building a Live Folder. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410

The Future of Android and the 1.5 SDK. . . . . . . . . . . . . . . . . . . . . . . . . . . . 421

Key Online Resources for the 1.5 SDK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424

INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425

15967fm.indd 13 6/5/09 11:14:51 AM

Tải ngay đi em, còn do dự, trời tối mất!