ebook img

Programming Android PDF

523 Pages·2012·8.22 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Programming Android

SECOND EDITION Programming Android Zigurd Mednieks, Laird Dornin, G. Blake Meike, and Ma- sumi Nakamura Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo Programming Android, Second Edition by Zigurd Mednieks, Laird Dornin, G. Blake Meike, and Masumi Nakamura 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 [email protected]. Proofreader: Sada Preisch Interior Designer: David Futato Illustrator: Rebecca Demarest Revision History for the : 2012-03-09 Early release revision 1 See http://oreilly.com/catalog/errata.csp?isbn=9781449316648 for release details. 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-31664-8 [LSI] 1331160433 Table of Contents Preface .................................................................... xiii Part I. Tools and Basics 1. Installing the Android SDK and Prerequisites ................................ 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 13 Making an Android Project 13 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 iii 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 79 Service 79 Content Providers 79 BroadcastReceiver 83 Component Life Cycles 83 The Activity Life Cycle 83 On Porting Software to Android 85 Static Application Resources and Context 86 Organizing Java Source 87 Resources 88 Application Manifests 90 Initialization Parameters in AndroidManifest.xml 91 Packaging an Android Application: The .apk File 94 The Android Application Runtime Environment 94 iv | Table of Contents The Dalvik VM 95 Zygote: Forking a New Process 95 Sandboxing: Processes and Users 95 The Android Libraries 96 Extending Android 98 The Android Application Template 98 Overrides and callbacks 98 Polymorphism and composition 101 Extending Android classes 102 Concurrency in Android 104 AsyncTask and the UI Thread 105 Threads in an Android Process 117 Serialization 118 Java Serialization 119 Parcelable 121 Classes That Support Serialization 124 Serialization and the Application Life Cycle 125 4. Getting Your Application into Users’ Hands ................................ 127 Application Signing 127 Public Key Encryption and Cryptographic Signing 127 How Signatures Protect Software Users, Publishers, and Secure Communications 129 Signing an Application 130 Placing an Application for Distribution in the Android Market 137 Becoming an Official Android Developer 138 Uploading Applications in the Market 138 Getting Paid 139 Google Maps API Keys 140 Specifying API-Level Compatibility 141 Compatibility with Many Kinds of Screens 141 Testing for Screen Size Compatibility 142 Resource Qualifiers and Screen Sizes 142 5. Eclipse for Android Software Development ................................ 143 Eclipse Concepts and Terminology 144 Plug-ins 144 Workspaces 145 Java Environments 146 Projects 147 Builders and Artifacts 147 Extensions 147 Associations 149 Table of Contents | v Eclipse Views and Perspectives 149 The Package Explorer View 150 The Task List View 150 The Outline View 151 The Problems View 152 Java Coding in Eclipse 152 Editing Java Code and Code Completion 152 Refactoring 153 Eclipse and Android 154 Preventing Bugs and Keeping Your Code Clean 154 Static Analyzers 155 Applying Static Analysis to Android Code 159 Limitations of Static Analysis 162 Eclipse Idiosyncrasies and Alternatives 162 Part II. About the Android Framework 6. 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 Multiple Pointers and Gestures 187 Listening for Key Events 189 Choosing an Event Handler 189 Advanced Wiring: Focus and Threading 191 The Menu and the Action Bar 195 View Debugging and Optimization 199 7. Fragments and Multiplatform Support ................................... 203 Creating a Fragment 204 Fragment Life Cycle 207 The Fragment Manager 208 Fragment Transactions 209 The Support Package 214 Fragments and Layout 215 vi | Table of Contents 8. Drawing 2D and 3D Graphics ............................................ 223 Rolling Your Own Widgets 223 Layout 224 Canvas Drawing 229 Drawables 240 Bitmaps 245 Bling 246 Shadows, Gradients, Filters and Hardware Acceleration 249 Animation 251 OpenGL Graphics 256 9. Handling and Persisting Data ........................................... 261 Relational Database Overview 261 SQLite 262 The SQL Language 262 SQL Data Definition Commands 263 SQL Data Manipulation Commands 266 Additional Database Concepts 268 Database Transactions 269 Example Database Manipulation Using sqlite3 269 SQL and the Database-Centric Data Model for Android Applications 273 The Android Database Classes 274 Database Design for Android Applications 275 Basic Structure of the SimpleVideoDbHelper Class 275 Using the Database API: MJAndroid 278 Android and Social Networking 278 The Source Folder (src) 280 Loading and Starting the Application 281 Database Queries and Reading Data from the Database 281 Modifying the Database 285 Part III. A Skeleton Application for Android 10. A Framework for a Well-Behaved Application .............................. 293 Visualizing Life Cycles 294 Visualizing the Activity Life Cycle 294 Visualizing the Fragment Life Cycle 306 The Activity Class and Well-Behaved Applications 309 The Activity Life Cycle and the User Experience 310 Life Cycle Methods of the Application Class 310 Table of Contents | vii 11. Building a User Interface ............................................... 315 Top-level design 315 Fragment, Activity, and scalable design 315 Action Bar 315 Tabs and swipe navigation 315 Visual editing of user interfaces 316 Starting With a Blank Slate 316 Picking a Simple Strategy 316 Create "Stub" Fragment Subclasses 316 Laying-out the Fragments 316 Lay-out Fragments Using the Visual Editor 316 Create Layouts Within Fragments 317 Folding and Unfolding a Scalable UI 317 Implementing and Scaling an Action Bar 317 Refactoring a User Interface Specification 317 Creating and Editing Assets 317 A Flowing and Intuitive User Experience Across Activities 317 Multitasking in a Small-Screen Environment 317 Tasks and Applications 318 Specifying Launch and Task Behavior 318 Creating a user interface for our example 322 Declarative and Programmatic UI creation 322 12. Using Content Providers ............................................... 325 Understanding Content Providers 326 Implementing a Content Provider 327 Browsing Video with Finch 328 Defining a Provider Public API 329 Defining the CONTENT_URI 330 Creating the Column Names 332 Declaring Column Specification Strings 332 Writing and Integrating a Content Provider 334 Common Content Provider Tasks 334 File Management and Binary Data 336 Android MVC and Content Observation 338 A Complete Content Provider: The SimpleFinchVideoContentProvider Code 339 The SimpleFinchVideoContentProvider Class and Instance Variables 339 Implementing the onCreate Method 341 Implementing the getType Method 342 Implementing the Provider API 342 Determining How Often to Notify Observers 347 Declaring Your Content Provider 347 viii | Table of Contents 13. A Content Provider As a Facade for a RESTful Web Service .................... 349 Developing RESTful Android Applications 350 A “Network MVC” 351 Summary of Benefits 353 Code Example: Dynamically Listing and Caching YouTube Video Content 354 Structure of the Source Code for the Finch YouTube Video Example 355 Stepping Through the Search Application 356 Step 1: Our UI Collects User Input 357 Step 2: Our Controller Listens for Events 357 Step 3: The Controller Queries the Content Provider with a managedQuery on the Content Provider/Model 358 Step 4: Implementing the RESTful Request 358 Constants and Initialization 358 Creating the Database 359 A Networked Query Method 359 insert and ResponseHandlers 372 File Management: Storing Thumbnails 373 14. Remote Procedure Calls - Building Android APIs ............................ 377 Part IV. Advanced Topics 15. Search .............................................................. 381 Search Interface 381 Search Basics 381 Search Dialog 387 Search Widget 388 Query Suggestions 389 Recent Query Suggestions 390 Custom Query Suggestions 391 16. Location and Mapping ................................................. 397 Location-Based Services 398 Mapping 399 The Google Maps Activity 399 The MapView and MapActivity 400 Working with MapViews 401 MapView and MyLocationOverlay Initialization 401 Pausing and Resuming a MapActivity 404 Controlling the Map with Menu Buttons 405 Controlling the Map with the Keypad 407 Table of Contents | ix

Description:
The Android Development Toolkit (ADT) Plug-in for Eclipse. 9 Basic Multithreaded Concurrent Programming in Java. 68 .. Sumita Mukherji, Adam Zaremba and the rest of the O'Reilly production team, and all . ing either Classic or the Java Developers package (EE or Standard) makes the most.
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.