1. Trang chủ
  2. » Giáo án - Bài giảng

professional android application development meier 2008 11 24 Lập trình android

420 4 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 420
Dung lượng 5,86 MB

Nội dung

Professional Android™ Application Development Reto Meier 44712ffirs.indd iii CuuDuongThanCong.com 10/20/08 4:08:56 PM Professional Android ™ Application Development Published by Wiley Publishing, Inc 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2009 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-0-470-34471-2 Manufactured in the United States of America 10 Library of Congress Cataloging-in-Publication Data is available from the publisher No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600 Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at http://www.wiley.com/go/permissions Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose No warranty may be created or extended by sales or promotional materials The advice and strategies contained herein may not be suitable for every situation This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services If professional assistance is required, the services of a competent professional person should be sought Neither the publisher nor the author shall be liable for damages arising herefrom The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read For general information on our other products and services please contact our Customer Care Department within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002 Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc and/or its affiliates, in the United States and other countries, and may not be used without written permission Android is a trademark of Google, Inc All other trademarks are the property of their respective owners Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book Wiley also publishes its books in a variety of electronic formats Some content that appears in print may not be available in electronic books 44712ffirs.indd iv CuuDuongThanCong.com 10/20/08 4:08:56 PM About the A uthor Originally from Perth, Western Australia, Reto Meier now lives in London Reto is an experienced software developer with more than 10 years of experience in GUI application architecture, design, and development He’s worked in various industries, including offshore oil and gas, before moving to London and into finance Always interested in emerging technologies, Reto has been involved in Android since the initial release in 2007 In his spare time, he tinkers with a wide range of development platforms including WPF and Google’s plethora of developer tools You can check out Reto’s web site, The Radioactive Yak, at http://blog.radioactiveyak.com About the T ech Edit or Dan Ulery is a software engineer with experience in NET, Java, and PHP development, as well as in deployment engineering He graduated from the University of Idaho with a bachelor of science degree in computer science and a minor in mathematics 44712ffirs.indd vi CuuDuongThanCong.com 10/20/08 4:08:56 PM Credits Executive Editor Vice President and Executive Group Publisher Chris Webb Richard Swadley Development Editor Vice President and Executive Publisher William Bridges Joseph B Wikert Technical Editor Project Coordinator, Cover Daniel Ulery Lynsey Stanford Senior Production Editor Compositor Debra Banninger James D Kramer, Happenstance Type-O-Rama Copy Editor Proofreader Cate Caffrey Nancy Carrasco Editorial Manager Indexer Mary Beth Wakefield Jack Lewis Production Manager Tim Tate 44712ffirs.indd vii CuuDuongThanCong.com 10/20/08 4:08:56 PM Acknowledgments A big thank you goes to the Android team, particularly those of you who’ve contributed to the Android developer Google Groups, for creating and supporting an exciting new playground I also thank Philipp Lenssen for providing an inspiration, and occasional venue, for my blogging efforts; Chris Webb for reading a blog and seeing an author; and Bill Bridges, Dan Ulery, and the Wrox team for helping me along the way Thanks also to Paul, Stu, and Mike: Your friendship and inspiration helped me get to where I am Most importantly, I’d like to thank Kristy For everything 44712ffirs.indd viii CuuDuongThanCong.com 10/20/08 4:08:56 PM Contents Introduction xvii Chapter 1: Hello, Android A Little Bac kground The Not So Distant Past The Future What It Is n’t An Op en Platform for Mobile Development Native Android Ap plications Android SDK Features Access to Hardware including Camera, GPS, and Accelerometer Native Google Maps, Geocoding, and Location-Based Services Background Services SQLite Database for Data Storage and Retrieval Shared Data and Interapplication Communication P2P Services with Google Talk Extensive Media Support and 2D/3D Graphics Optimized Memory and Process Management Introducing the Op en Handset Allia nce What Doe s Android Run O n? Why Develop for Android? What Will Drive Android Adoption? What Does It Have That Others Don’t? Changing the Mobile Development Landscape 2 3 4 6 7 8 9 10 10 11 Introducing the De velopment Framework 11 What Comes in the Box Understanding the Android Software Stack The Dalvik Virtual Machine Android Application Architecture Android Libraries Advanced Android Libraries 12 12 14 14 15 16 Summary 44712ftoc.indd ix CuuDuongThanCong.com 17 10/21/08 12:07:15 AM Contents Chapter : Getting Star ted Developing for Android What You Need to Begin Creating Your First Android Activity Types of Android Applications Developing for Mobile Devices Hardware-Imposed Design Considerations Considering the Users’ Environment Developing for Android To-Do List E xample Android Development Tools The Android Emulator Dalvik Debug Monitor Service (DDMS) The Android Debug Bridge (ADB) Summary Chapter 3: Creating Ap plications and Activities 19 20 20 24 29 30 30 33 34 37 42 42 43 43 44 45 What Mak es an Android Ap plication? Introducing the Ap plication Ma nifest Using the Ma nifest Edit or The Android Ap plication Life Cy cle Understanding Application Priority and Process Stat es Externalizing Resources 46 46 49 50 51 52 Creating Resources Using Resources To-Do List Resources Example Creating Resources for Different Languages and Hardware Runtime Configuration Changes 53 59 62 63 64 A Closer L ook at Android A ctivities Creating an Activity The Activity Life Cycle Android Activity Classes Summary Chapter 4: Creating User Interfaces Fundamental Android U I Design Introducing Vie ws Creating Activity User Interfaces with Views The Android Widget Toolbox 66 66 68 73 73 75 76 76 77 78 x 44712ftoc.indd x CuuDuongThanCong.com 10/21/08 12:07:15 AM Contents Introducing L ayouts Using Layouts Creating Ne w Views Modifying Existing Views Creating Compound Controls Creating Custom Widgets and Controls Using Custom Controls Creating a nd Using Menu s Introducing the Android Menu System Defining an Activity Menu Dynamically Updating Menu Items Handling Menu Selections Submenus and Context Menus To-Do List Example Continued Summary Chapter 5: Intents, Broadcast Receivers, Adapters, and the Int ernet Introducing Intents Using Using Using Using Intents to Launch Activities Intent Filters to Service Implicit Intents Intent Filters for Plug-ins and Extensibility Intents to Broadcast Events Introducing Adapters Introducing Some Android-Supplied Adapters Using Adapters for Data Binding Using Int ernet Resources Connecting to an Internet Resource Leveraging Internet Resources Introducing D ialogs 79 79 80 81 85 88 98 99 99 101 104 104 105 107 12 113 114 114 121 130 132 136 136 136 141 142 143 143 Introducing the Dialog Class Using Activities as Dialogs 144 147 Creating a n Earthquake Viewer Summary 148 57 Chapter 6: Data St orage, Retrieval, and Sharing Android Techniques for Saving Data Saving Simple Application Data Creating and Saving Preferences Retrieving Shared Preferences Saving the Activity State Creating a Preferences Page for the Earthquake Viewer 159 160 160 161 161 162 165 xi 44712ftoc.indd xi CuuDuongThanCong.com 10/21/08 12:07:15 AM Contents Saving and Loading Files Including Static Files as Resources File Management Tools Databases in Android Introducing SQLite Cursors and Content Values Working with Android Databases Introducing Cont ent Providers Using Content Providers Native Android Content Providers Creating a New Content Provider Creating and Using an Earthquake Content Provider 174 174 175 175 176 176 177 189 189 192 194 197 Summary 205 Chapter 7: Maps, G eocoding, and Location-Based Services Using Location-Based Se rvices Setting up the E mulator with T est P roviders Updating Locations in Emulator Location Providers Create an Application to Manage Test Location Providers Selecting a L ocation P rovider Finding the Available Providers Finding Providers Based on Requirement Criteria Finding Your Location “Where Am I?” Example Tracking Movement Updating Your Location in “Where Am I?” Using Proximity Aler ts Using the G eocoder Reverse Geocoding Forward Geocoding Geocoding “Where Am I?” Creating Map -Based A ctivities Introducing MapView and MapActivity Creating a Map-Based Activity Configuring and Using Map Views Using the Map Controller Mapping “Where Am I?” Creating and Using Overlays Introducing MyLocationOverlay Introducing ItemizedOverlays and OverlayItems Pinning Views to the Map and Map Positions 207 208 208 208 209 212 212 212 213 214 216 217 219 220 221 221 222 224 224 224 226 227 228 231 239 239 240 xii 44712ftoc.indd xii CuuDuongThanCong.com 10/21/08 12:07:15 AM Contents Mapping Earthquakes E xample Summary Chapter : Working in the Bac kground Introducing Services 242 47 249 250 Creating and Controlling Services Binding Activities to Services 250 258 Using Background Worker Threads 259 Creating New Threads Synchronizing Threads for GUI Operations Moving the Earthquake Service to a Background Thread Let’s Make a Toast Customizing Toasts Using Toasts in Worker Threads Introducing Noti f cations Introducing the Notification Manager Creating Notifications Triggering Notifications Adding Notifications to the Earthquake Monitor Advanced Notification Techniques Ongoing and Insistent Notifications Using Alarms Using Alarm s to Update Earthquakes Summary 260 260 261 262 263 264 265 266 266 267 267 270 272 273 274 76 Chapter : Peer-to-Peer Communication 279 Introducing Android In stant Me ssaging 280 Using the GTalk Service Binding to the GTalk Service Making a GTalk Connection and Starting an IM Session Introducing Presence and the Contact Roster Managing Chat Sessions Sending and Receiving Data Messages Introducing SMS Using SMS in Your Application Sending SMS Messages Listening for SMS Messages Emergency Responder SMS Example Automating the Emergency Responder Summary 31 280 281 282 283 286 289 291 291 292 294 297 306 xiii 44712ftoc.indd xiii CuuDuongThanCong.com 10/21/08 12:07:15 AM ... Indiana Published simultaneously in Canada ISBN: 97 8-0 -4 7 0-3 447 1-2 Manufactured in the United States of America 10 Library of Congress Cataloging-in-Publication Data is available from the publisher... WebKit-based browser ❑ Full support for applications that integrate Map controls as part of their user interface ❑ Peer-to-peer (P2P) support using Google Talk ❑ Mobile-optimized hardware-accelerated... books have similar titles, you may find it easiest to search by ISBN; this book’s ISBN is 97 8-0 -4 7 0-3 447 1-2 Once you download the code, just decompress it with your favorite compression tool Alternatively,

Ngày đăng: 29/08/2020, 16:09

w