首页 > > > Programming Android.pdf

Programming Android.pdf

Programming Android.pdf

上传者: 陆永 2011-10-13 评分1 评论0 下载54 收藏10 阅读量1023 暂无简介 简介 举报

简介:本文档为《Programming Androidpdf》,可适用于软件工程领域,主题内容包含ProgrammingAndroidProgrammingAndroidZigurdMednieks,LairdDornin,GBlakeMeike符等。

Programming Android Programming Android Zigurd Mednieks, Laird Dornin, G. Blake Meike, and Masumi Nakamura Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo Programming Android by Zigurd Mednieks, Laird Dornin, G. Blake Meike, and Masumi Nakamura Copyright 2011 Zigurd Mednieks, Laird Dornin, G. Blake Meike, and Masumi Nakamura. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com. Editors: Andy Oram and Brian Jepson Production Editor: Adam Zaremba Copyeditor: Audrey Doyle Technical Editors: Vijay S. Yellapragada and Johan van der Hoeven Proofreader: Sada Preisch Indexer: Lucie Haskins Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Rebecca Demarest Printing History: July 2011: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Programming Android, the image of a pine grosbeak, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. ISBN: 978-1-449-38969-7 [LSI] 1310671393 Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Part I. Tools and Basics 1. Your Toolkit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Installing the Android SDK and Prerequisites 3 The Java Development Kit (JDK) 4 The Eclipse Integrated Development Environment (IDE) 5 The Android SDK 7 Adding Build Targets to the SDK 8 The Android Development Toolkit (ADT) Plug-in for Eclipse 9 Test Drive: Confirm That Your Installation Works 12 Making an Android Project 12 Making an Android Virtual Device (AVD) 16 Running a Program on an AVD 19 Running a Program on an Android Device 20 Troubleshooting SDK Problems: No Build Targets 21 Components of the SDK 21 The Android Debug Bridge (adb) 21 The Dalvik Debug Monitor Server (DDMS) 21 Components of the ADT Eclipse Plug-in 23 Android Virtual Devices 25 Other SDK Tools 26 Keeping Up-to-Date 28 Keeping the Android SDK Up-to-Date 28 Keeping Eclipse and the ADT Plug-in Up-to-Date 29 Keeping the JDK Up-to-Date 29 Example Code 30 SDK Example Code 30 Example Code from This Book 30 On Reading Code 32 v 2. Java for Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Android Is Reshaping Client-Side Java 33 The Java Type System 34 Primitive Types 34 Objects and Classes 35 Object Creation 35 The Object Class and Its Methods 37 Objects, Inheritance, and Polymorphism 39 Final and Static Declarations 41 Abstract Classes 45 Interfaces 46 Exceptions 48 The Java Collections Framework 52 Garbage Collection 55 Scope 56 Java Packages 56 Access Modifiers and Encapsulation 57 Idioms of Java Programming 59 Type Safety in Java 59 Using Anonymous Classes 62 Modular Programming in Java 65 Basic Multithreaded Concurrent Programming in Java 68 Synchronization and Thread Safety 68 Thread Control with wait() and notify() Methods 71 Synchronization and Data Structures 73 3. The Ingredients of an Android Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Traditional Programming Models Compared to Android 75 Activities, Intents, and Tasks 77 Other Android Components 78 Service 79 Content Providers 79 BroadcastReceiver 82 Static Application Resources and Context 82 Application Manifests 83 A Typical Source Tree 84 Initialization Parameters in AndroidManifest.xml 84 Resources 87 The Android Application Runtime Environment 88 The Dalvik VM 89 Zygote: Forking a New Process 89 Sandboxing: Processes and Users 89 Component Life Cycles 90 vi | Table of Contents The Activity Life Cycle 90 Packaging an Android Application: The .apk File 92 On Porting Software to Android 93 4. Getting Your Application into Users’ Hands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Application Signing 95 Public Key Encryption and Cryptographic Signing 95 How Signatures Protect Software Users, Publishers, and Secure Communications 97 Signing an Application 98 Placing an Application for Distribution in the Android Market 105 Becoming an Official Android Developer 106 Uploading Applications in the Market 106 Getting Paid 107 Google Maps API Keys 108 Specifying API-Level Compatibility 109 Compatibility with Many Kinds of Screens 109 Testing for Screen Size Compatibility 110 Resource Qualifiers and Screen Sizes 110 5. Eclipse for Android Software Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Eclipse Concepts and Terminology 112 Plug-ins 112 Workspaces 113 Java Environments 114 Projects 115 Builders and Artifacts 115 Extensions 115 Associations 117 Eclipse Views and Perspectives 117 The Package Explorer View 118 The Task List View 118 The Outline View 119 The Problems View 120 Java Coding in Eclipse 120 Editing Java Code and Code Completion 120 Refactoring 121 Eclipse and Android 122 Preventing Bugs and Keeping Your Code Clean 122 Static Analyzers 123 Applying Static Analysis to Android Code 127 Limitations of Static Analysis 130 Eclipse Idiosyncrasies and Alternatives 130 Table of Contents | vii 6. Effective Java for Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 The Android Framework 133 The Android Libraries 133 Extending Android 135 Organizing Java Source 140 Concurrency in Android 142 AsyncTask and the UI Thread 143 Threads in an Android Process 154 Serialization 156 Java Serialization 157 Parcelable 159 Classes That Support Serialization 162 Serialization and the Application Life Cycle 163 Part II. About the Android Framework 7. Building a View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Android GUI Architecture 167 The Model 167 The View 168 The Controller 169 Putting It Together 169 Assembling a Graphical Interface 171 Wiring Up the Controller 176 Listening to the Model 178 Listening for Touch Events 183 Listening for Key Events 186 Alternative Ways to Handle Events 187 Advanced Wiring: Focus and Threading 189 The Menu 193 8. Fragments and Multiplatform Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Creating a Fragment 198 Fragment Life Cycle 201 The Fragment Manager 202 Fragment Transactions 203 The Compatibility Package 208 9. Drawing 2D and 3D Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Rolling Your Own Widgets 211 Layout 212 Canvas Drawing 217 viii | Table of Contents Drawables 228 Bitmaps 232 Bling 234 Shadows, Gradients, and Filters 237 Animation 238 OpenGL Graphics 243 10. Handling and Persisting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Relational Database Overview 247 SQLite 248 The SQL Language 248 SQL Data Definition Commands 249 SQL Data Manipulation Commands 252 Additional Database Concepts 254 Database Transactions 255 Example Database Manipulation Using sqlite3 255 SQL and the Database-Centric Data Model for Android Applications 258 The Android Database Classes 259 Database Design for Android Applications 260 Basic Structure of the SimpleVideoDbHelper Class 261 Using the Database API: MJAndroid 264 Android and Social Networking 264 The Source Folder (src) 265 Loading and Starting the Application 267 Database Queries and Reading Data from the Database 267 Modifying the Database 271 Part III. A Skeleton Application for Android 11. A Framework for a Well-Behaved Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Visualizing Life Cycles 280 Visualizing the Activity Life Cycle 280 Visualizing the Fragment Life Cycle 292 The Activity Class and Well-Behaved Applications 295 The Activity Life Cycle and the User Experience 296 Life Cycle Methods of the Application Class 296 A Flowing and Intuitive User Experience Across Activities 299 Multitasking in a Small-Screen Environment 299 Tasks and Applications 299 Specifying Launch and Task Behavior 300 Table of Contents | ix 12. Using Content Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 Understanding Content Providers 306 Implementing a Content Provider 307 Browsing Video with Finch 308 Defining a Provider Public API 309 Defining the CONTENT_URI 310 Creating the Column Names 312 Declaring Column Specification Strings 312 Writing and Integrating a Content Provider 314 Common Content Provider Tasks 314 File Management and Binary Data 316 Android MVC and Content Observation 318 A Complete Content Provider: The SimpleFinchVideoContentProvider Code 319 The SimpleFinchVideoContentProvider Class and Instance Variables 319 Implementing the onCreate Method 321 Implementing the getType Method 322 Implementing the Provider API 322 Determining How Often to Notify Observers 327 Declaring Your Content Provider 327 13. Exploring Content Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 Developing RESTful Android Applications 330 A “Network MVC” 331 Summary of Benefits 333 Code Example: Dynamically Listing and Caching YouTube Video Content 334 Structure of the Source Code for the Finch YouTube Video Example 335 Stepping Through the Search Application 336 Step 1: Our UI Collects User Input 337 Step 2: Our Controller Listens for Events 337 Step 3: The Controller Queries the Content Provider with a managedQuery on the Content Provider/Model 338 Step 4: Implementing the RESTful Request 338 Constants and Initialization 338 Creating the Database 339 A Networked Query Method 339 insert and ResponseHandlers 352 File Management: Storing Thumbnails 353 x | Table of Contents Part IV. Advanced Topics 14. Multimedia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 Audio and Video 359 Playing Audio and Video 360 Audio Playback 361 Video Playback 363 Recording Audio and Video 364 Audio Recording 365 Video Recording 368 Stored Media Content 369 15. Location and Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 Location-Based Services 372 Mapping 373 The Google Maps Activity 373 The MapView and MapActivity 374 Working with MapViews 375 MapView and MyLocationOverlay Initialization 375 Pausing and Resuming a MapActivity 378 Controlling the Map with Menu Buttons 379 Controlling the Map with the Keypad 381 Location Without Maps 382 The Manifest and Layout Files 382 Connecting to a Location Provider and Getting Location Updates 383 Updating the Emulated Location 386 16. Sensors, NFC, Speech, Gestures, and Accessibility . . . . . . . . . . . . . . . . . . . . . . . . . . . 391 Sensors 391 Position 393 Other Sensors 395 Near Field Communication (NFC) 396 Reading a Tag 396 Writing to a Tag 403 P2P Mode 405 Gesture Input 406 Accessibility 407 17. Communication, Identity, Sync, and Social Media . . . . . . . . . . . . . . . . . . . . . . . . . . 411 Account Contacts 411 Authentication and Synchronization 414 Authentication 415 Table of Contents | xi Synchronization 422 Bluetooth 429 The Bluetooth Protocol Stack 429 Bluez: The Linux Bluetooth Implementation 431 Using Bluetooth in Android Applications 431 18. The Android Native Development Kit (NDK) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 Native Methods and JNI Calls 446 Conventions on the Native Method Side 446 Conventions on the Java Side 447 The Android NDK 448 Setting Up the NDK Environment 448 Compiling with the NDK 448 JNI, NDK, and SDK: A Sample App 449 Android-Provided Native Libraries 451 Building Your Own Custom Library Modules 453 Native Activities 456 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463 xii | Table of Contents Preface The purpose of this book is to enable you to create well-engineered Android applica- tions that go beyond the scope of small example applications. This book is for people coming to Android programming from a variety of backgrounds. If you have been programming iPhone or Mac OS applications in Objective-C, you will find coverage of Android tools and Java language features relevant to Android pro- gramming that will help you bring your knowledge of mobile application development to Android. If you are an experienced Java coder, you will find coverage of Android application architecture that will enable you to use your Java expertise in this newly vibrant world of client Java application development. In short, this is a book for people with some relevant experience in object-oriented languages, mobile applications, REST applications, and similar disciplines who want to go further than an introductory book or online tutorials will take them. How This Book Is Organized We want to get you off to a fast start. The chapters in the first part of this book will step you through using the SDK tools so that you can access example code in this book and in the SDK, even as you expand your knowledge of SDK tools, Java, and database design. The tools and basics covered in the first part might be familiar enough to you that you would want to skip to Part II where we build foundational knowledge for developing larger Android applications. The central part of this book is an example of an application that uses web services to deliver information to the user—something many applications have at their core. We present an application architecture, and a novel approach to using Android’s frame- work classes that enables you to do this particularly efficiently. You will be able to use this application as a framework for creating your own applications, and as a tool for learning about Android programming. In the final part of this book, we explore Android APIs in specific application areas: multimedia, location, sensors, and communication, among others, in order to equip you to program applications in your specific area of interest. xiii By the time you reach the end of this book, we want you to have gained knowledge beyond reference material and a walk-through of examples. We want you to have a point of view on how to make great Android applications. Conventions Used in This Book The following typographical conventions are used in this book: Italic Indicates new terms, URLs, email addresses, filenames, and file extensions Constant width Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords Constant width bold Shows commands or other text that should be typed literally by the user Constant width italic Shows text that should be replaced with user-supplied values or by values deter- mined by context This icon signifies a tip, suggestion, or general note. This icon indicates a warning or caution. Using Code Examples This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission. We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Programming Android by Zigurd xiv | Preface Mednieks, Laird Dornin, G. Blake Meike, and Masumi Nakamura. Copyright 2011 O’Reilly Media, Inc., 978-1-449-38969-7.” If you feel your use of code examples falls outside fair use or the permission given here, feel free to contact us at permissions@oreilly.com. Safari Books Online Safari Books Online is an on-demand digital library that lets you easily search more than 7,500 technology and creative reference books and vid- eos to find the answers you need quickly. With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, down- load chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features. O’Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O’Reilly and other pub- lishers, sign up for free at http://my.safaribooksonline.com. How to Contact Us Please address comments and questions concerning this book to the publisher: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 800-998-9938 (in the United States or Canada) 707-829-0515 (international or local) 707-829-0104 (fax) We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at: http://oreilly.com/catalog/9781449389697 To comment or ask technical questions about this book, send email to: bookquestions@oreilly.com For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com. Find us on Facebook: http://facebook.com/oreilly Follow us on Twitter: http://twitter.com/oreillymedia Preface | xv Watch us on YouTube: http://www.youtube.com/oreillymedia Acknowledgments The authors have adapted portions of this book from their previously released title, Android Application Development (O’Reilly). Drafts of this book were released on the O’Reilly Open Feedback Publishing System (OFPS) in order to get your feedback on whether and how we are meeting the goals for this book. We are very grateful for the readers who participated in OFPS, and we owe them much in correcting our errors and improving our writing. Open review of drafts will be part of future editions, and we welcome your views on every aspect of this book. xvi | Preface PART I Tools and Basics Part I shows you how to install and use your tools, what you need to know about Java to write good Android code, and how to design and use SQL databases, which are central to the Android application model, persistence system, and implementation of key design patterns in Android programs. CHAPTER 1 Your Toolkit This chapter shows you how to install the Android software development kit (SDK) and all

该用户的其他资料

  • 名称/格式
  • 评分
  • 下载次数
  • 资料大小
  • 上传时间

用户评论

0/200
    暂无评论
上传我的资料

相关资料

资料评价:

/ 502
所需积分:1 立即下载
返回
顶部
举报
资料
关闭

温馨提示

感谢您对爱问共享资料的支持,精彩活动将尽快为您呈现,敬请期待!