wrox press professional android application development (2009)

420 219 0
  • Loading ...
1/420 trang
Tải xuống

Thông tin tài liệu

Ngày đăng: 31/03/2014, 16:39

ProfessionalAndroid™ Application DevelopmentReto Meier44712ffirs.indd iii44712ffirs.indd iii 10/20/08 4:08:56 PM10/20/08 4:08:56 PMProfessional Android™ Application DevelopmentPublished byWiley Publishing, Inc.10475 Crosspoint BoulevardIndianapolis, IN 46256www.wiley.comCopyright © 2009 by Wiley Publishing, Inc., Indianapolis, IndianaPublished simultaneously in CanadaISBN: 978-0-470-34471-2Manufactured in the United States of America10 9 8 7 6 5 4 3 2 1Library 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 permis-sion 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 war-ranties with respect to the accuracy or completeness of the contents of this work and specifi cally disclaim all warranties, including without limitation warranties of fi tness 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 affi liates, 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 iv44712ffirs.indd iv 10/20/08 4:08:56 PM10/20/08 4:08:56 PMAbout the A uthorOriginally 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 fi nance.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 EditorDan 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 vi44712ffirs.indd vi 10/20/08 4:08:56 PM10/20/08 4:08:56 PMCreditsExecutive EditorChris WebbDevelopment EditorWilliam BridgesTechnical EditorDaniel UlerySenior Production EditorDebra BanningerCopy EditorCate CaffreyEditorial ManagerMary Beth Wakefi eld Production ManagerTim TateVice President and Executive Group PublisherRichard SwadleyVice President and Executive PublisherJoseph B. WikertProject Coordinator, CoverLynsey StanfordCompositorJames D. Kramer, Happenstance Type-O-RamaProofreaderNancy CarrascoIndexerJack Lewis44712ffirs.indd vii44712ffirs.indd vii 10/20/08 4:08:56 PM10/20/08 4:08:56 PMAcknowledgmentsA 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 viii44712ffirs.indd viii 10/20/08 4:08:56 PM10/20/08 4:08:56 PMContentsIntroduction xviiHello, Android Chapter 1: 1A Little Bac kground 2The Not So Distant Past 2The Future 3What It Is n’t 3An Open Platform for Mobile Development 4Native Android Ap plications 4Android SDK Features 5Access to Hardware including Camera, GPS, and Accelerometer 6Native Google Maps, Geocoding, and Location-Based Services 6Background Services 6SQLite Database for Data Storage and Retrieval 7Shared Data and Interapplication Communication 7P2P Services with Google Talk 7Extensive Media Support and 2D/3D Graphics 8Optimized Memory and Process Management 8Introducing the Op en Handset Allia nce 8What Doe s Android Run O n? 9Why Develop for Android? 9What Will Drive Android Adoption? 10What Does It Have That Others Don’t? 10Changing the Mobile Development Landscape 11Introducing the De velopment Framework 11What Comes in the Box 12Understanding the Android Software Stack 12The Dalvik Virtual Machine 14Android Application Architecture 14Android Libraries 15Advanced Android Libraries 16Summary 1744712ftoc.indd ix44712ftoc.indd ix 10/21/08 12:07:15 AM10/21/08 12:07:15 AMxContentsGetting Star ted 1Chapter 2: 9Developing for Android 20What You Need to Begin 20Creating Your First Android Activity 24Types of Android Applications 29Developing for Mobile Devices 30Hardware-Imposed Design Considerations 30Considering the Users’ Environment 33Developing for Android 34To-Do List E xample 37Android Development Tools 42The Android Emulator 42Dalvik Debug Monitor Service (DDMS) 43The Android Debug Bridge (ADB) 43Summary 44Creating Ap plications and Activities 4Chapter 3: 5What Makes an Android Ap plication? 46Introducing the Ap plication Manifest 46Using the Ma nifest Edit or 49The Android Ap plication Life Cycle 50Understanding Application Priority and Process Stat es 51Externalizing Resources 52Creating Resources 53Using Resources 59To-Do List Resources Example 62Creating Resources for Different Languages and Hardware 63Runtime Confi guration Changes 64A Closer L ook at Android A ctivities 66Creating an Activity 66The Activity Life Cycle 68Android Activity Classes 73Summary 73Creating User Interfaces 7Chapter 4: 5Fundamental Android U I Design 76Introducing Vie ws 76Creating Activity User Interfaces with Views 77The Android Widget Toolbox 7844712ftoc.indd x44712ftoc.indd x 10/21/08 12:07:15 AM10/21/08 12:07:15 AMxiContentsIntroducing Layouts 79Using Layouts 79Creating New Views 80Modifying Existing Views 81Creating Compound Controls 85Creating Custom Widgets and Controls 88Using Custom Controls 98Creating and Using Menu s 99Introducing the Android Menu System 99Defi ning an Activity Menu 101Dynamically Updating Menu Items 104Handling Menu Selections 104Submenus and Context Menus 105To-Do List Example Continued 107Summary 1 12Intents, Broadcast Receivers, Adapters, and the Int ernet 11Chapter 5: 3Introducing Intents 114Using Intents to Launch Activities 114Using Intent Filters to Service Implicit Intents 121Using Intent Filters for Plug-ins and Extensibility 130Using Intents to Broadcast Events 132Introducing Adapters 136Introducing Some Android-Supplied Adapters 136Using Adapters for Data Binding 136Using Internet Resources 141Connecting to an Internet Resource 142Leveraging Internet Resources 143Introducing Dialogs 143Introducing the Dialog Class 144Using Activities as Dialogs 147Creating an Earthquake Viewer 148Summary 1 57Data St orage, Retrieval, and Sharing 15Chapter 6: 9Android Techniques for Saving Data 160Saving Simple Application Data 160Creating and Saving Preferences 161Retrieving Shared Preferences 161Saving the Activity State 162Creating a Preferences Page for the Earthquake Viewer 16544712ftoc.indd xi44712ftoc.indd xi 10/21/08 12:07:15 AM10/21/08 12:07:15 AMxiiContentsSaving and Loading Files 174Including Static Files as Resources 174File Management Tools 175Databases in Android 175Introducing SQLite 176Cursors and Content Values 176Working with Android Databases 177Introducing Cont ent Providers 189Using Content Providers 189Native Android Content Providers 192Creating a New Content Provider 194Creating and Using an Earthquake Content Provider 197Summary 205Maps, G eocoding, and Location-Based Services 20Chapter 7: 7Using Location-Based Services 208Setting up the E mulator with T est P roviders 208Updating Locations in Emulator Location Providers 208Create an Application to Manage Test Location Providers 209Selecting a L ocation Provider 212Finding the Available Providers 212Finding Providers Based on Requirement Criteria 212Finding Your Location 213“Where Am I?” Example 214Tracking Movement 216Updating Your Location in “Where Am I?” 217Using Proximity Aler ts 219Using the G eocoder 220Reverse Geocoding 221Forward Geocoding 221Geocoding “Where Am I?” 222Creating Map -Based Activities 224Introducing MapView and MapActivity 224Creating a Map-Based Activity 224Confi guring and Using Map Views 226Using the Map Controller 227Mapping “Where Am I?” 228Creating and Using Overlays 231Introducing MyLocationOverlay 239Introducing ItemizedOverlays and OverlayItems 239Pinning Views to the Map and Map Positions 24044712ftoc.indd xii44712ftoc.indd xii 10/21/08 12:07:15 AM10/21/08 12:07:15 AMxiiiContentsMapping Earthquakes E xample 242Summary 2 47Working in the Bac kground 24Chapter 8 : 9Introducing Services 250Creating and Controlling Services 250Binding Activities to Services 258Using Background Worker Threads 259Creating New Threads 260Synchronizing Threads for GUI Operations 260Moving the Earthquake Service to a Background Thread 261Let’s Make a Toast 262Customizing Toasts 263Using Toasts in Worker Threads 264Introducing Noti f cations 265Introducing the Notifi cation Manager 266Creating Notifi cations 266Triggering Notifi cations 267Adding Notifi cations to the Earthquake Monitor 267Advanced Notifi cation Techniques 270Ongoing and Insistent Notifi cations 272Using Alarms 273Using Alarms to Update Earthquakes 274Summary 2 76Peer-to-Peer Communication 27Chapter 9 : 9Introducing Android In stant Messaging 280Using the GTalk Service 280Binding to the GTalk Service 281Making a GTalk Connection and Starting an IM Session 282Introducing Presence and the Contact Roster 283Managing Chat Sessions 286Sending and Receiving Data Messages 289Introducing SMS 291Using SMS in Your Application 291Sending SMS Messages 292Listening for SMS Messages 294Emergency Responder SMS Example 297Automating the Emergency Responder 306Summary 31 444712ftoc.indd xiii44712ftoc.indd xiii 10/21/08 12:07:15 AM10/21/08 12:07:15 AM[...]... The Android software development kit (SDK) includes everything you need to start developing, testing, and debugging Android applications Included in the SDK download are: ❑ The Android APIs The core of the SDK is the Android API libraries that provide developer access to the Android stack These are the same libraries used at Google to create native Android applications ❑ Development Tools To turn Android. .. introduces Android, including what it is and how it fits into existing mobile development What Android offers as a development platform and why it’s an exciting opportunity for creating mobile phone applications are then examined in greater detail Chapter 2 covers some best practices for mobile development and explains how to download the Android SDK and start developing applications It also introduces the Android. .. executable Android applications, the SDK includes several development tools that let you compile and debug your applications You will learn more about the developer tools in Chapter 2 ❑ The Android Emulator The Android Emulator is a fully interactive Android device emulator featuring several alternative skins Using the emulator, you can see how your applications will look and behave on a real Android. .. used to create Android applications It also provides a generic abstraction for hardware access and manages the user interface and application resources ❑ Application Layer All applications, both native and third party, are built on the application layer using the same API libraries The application layer runs within the Android run time using the classes and services made available from the application. .. Android phone an Android phone rather than a mobile Linux implementation is the Android run time Including the core libraries and the Dalvik virtual machine, the Android run time is the engine that powers your applications and, along with the libraries, forms the basis for the application framework ❑ Core Libraries While Android development is done in Java, Dalvik is not a Java VM The core Android libraries... picture viewer ❑ The Android Marketplace client for downloading thied-party Android applications ❑ The Amazon MP3 store client for purchasing DRM free music All the native applications are written in Java using the Android SDK and are run on Dalvik The data stored and used by the native applications — like contact details — are also available to thirdparty applications Similarly, your applications can... streamed media ❑ android. opengl Android offers a powerful 3D rendering engine using the OpenGL ES API that you can use to create dynamic 3D user interfaces for your applications ❑ android. hardware Where available, the hardware API exposes sensor hardware including the camera, accelerometer, and compass sensors as shown in Chapter 10 ❑ android. bluetooth, android. net.wifi, and android. telephony Android also... graphics ❑ That all Android applications are built equal, allowing users to completely replace one application with another, including the replacement of the core native applications ❑ That the Android SDK includes developer tools, APIs, and comprehensive documentation The next chapter will help you get started by downloading and installing the Android SDK and setting up an Android development environment... learn how to use the Android developer tools plug-in to streamline development, testing, and debugging before creating your first Android application After learning about the building blocks of Android applications, you’ll be introduced to the different types of applications you can create, and you’ll start to understand some of the design considerations that should go into developing applications for mobile... Process Management Android s process and memory management is a little unusual Like Java and NET, Android uses its own run time and virtual machine to manage application memory Unlike either of these frameworks, the Android run time also manages the process lifetimes Android ensures application responsiveness by stopping and killing processes as necessary to free resources for higher-priority applications . Professional Android ™ Application Development Reto Meier 44712ffirs.indd iii44712ffirs.indd iii 10/20/08 4:08:56 PM10/20/08 4:08:56 PM Professional Android ™ Application Development Published. requirement. Android development is supported in Windows, MacOS, and Linux, with the SDK available from the Android web site. You do not need an Android device to use this book or develop Android applications. Chapter. Environment 33 Developing for Android 34 To-Do List E xample 37 Android Development Tools 42 The Android Emulator 42 Dalvik Debug Monitor Service (DDMS) 43 The Android Debug Bridge (ADB) 43 Summary
- Xem thêm -

Xem thêm: wrox press professional android application development (2009), wrox press professional android application development (2009), wrox press professional android application development (2009)

Từ khóa liên quan

Gợi ý tài liệu liên quan cho bạn

Nhận lời giải ngay chưa đến 10 phút Đăng bài tập ngay