Multithreading is essential if you want to create an Android app with a great user experience, but how do you know which techniques can help solve your problem? This practical book describes many asynchronous mechanisms available in the Android SDK, and provides guidelines for selecting the ones most appropriate for the app you’re building. Author Anders Goransson demonstrates the advantages and disadvantages of each technique, with sample code and detailed explanations for using it efficiently. The first part of the book describes the building blocks of asynchronous processing, and the second part covers Android libraries and constructs for developing fast, responsive, and well-structured apps. * Understand multithreading basics in Java and on the Android platform * Learn how threads communicate within and between processes * Use strategies to reduce the risk of memory leaks * Manage the lifecycle of a basic thread * Run tasks sequentially in the background with __HandlerThread__ * Use Java’s Executor Framework to control or cancel threads * Handle background task execution with __AsyncTask__ and __IntentService__ * Access content providers with __AsyncQueryHandler__ * Use loaders to update the UI with new data Copyright 4 Table of Contents 7 Preface 13 Audience 13 Contents of This Book 14 Conventions Used in this Book 15 Using Code Examples 16 Safari庐 Books Online 16 How to Contact Us 17 Acknowledgements 17 Chapter聽1.聽Android Components and the Need for Multiprocessing 19 Android Software Stack 19 Application Architecture 20 Application 21 Components 21 Application Execution 23 Linux Process 24 Lifecycle 24 Structuring Applications for Performance 27 Creating Responsive Applications Through Threads 27 Summary 29 Part聽I.聽Fundamentals 31 Chapter聽2.聽Multithreading in Java 33 Thread Basics 33 Execution 33 Single-Threaded Application 35 Multithreaded Application 35 Thread Safety 37 Intrinsic Lock and Java Monitor 38 Synchronize Access to Shared Resources 40 Example: Consumer and Producer 42 Task Execution Strategies 44 Concurrent Execution Design 45 Summary 45 Chapter聽3.聽Threads on Android 47 Android Application Threads 47 UI Thread 47 Binder Threads 48 Background Threads 48 The Linux Process and Threads 49 Scheduling 52 Summary 55 Chapter聽4.聽Thread Communication 57 Pipes 57 Basic Pipe Use 58 Example: Text Processing on a Worker Thread 60 Shared Memory 62 Signaling 63 BlockingQueue 64 Android Message Passing 65 Example: Basic Message Passing 67 Classes Used in Message Passing 69 Message 73 Looper 76 Handler 78 Removing Messages from the Queue 86 Observing the Message Queue 88 Communicating with the UI Thread 91 Summary 92 Chapter聽5.聽Interprocess Communication 93 Android RPC 93 Binder 94 AIDL 95 Synchronous RPC 97 Asynchronous RPC 99 Message Passing Using the Binder 101 One-Way Communication 102 Two-Way Communication 104 Summary 105 Chapter聽6.聽Memory Management 107 Garbage Collection 107 Thread-Related Memory Leaks 109 Thread Execution 110 Thread Communication 116 Avoiding Memory Leaks 119 Use Static Inner Classes 119 Use Weak References 119 Stop Worker Thread Execution 120 Retain Worker Threads 120 Clean Up the Message Queue 120 Summary 121 Part聽II.聽Asynchronous Techniques 123 Chapter聽7.聽Managing the Lifecycle of a Basic Thread 125 Basics 125 Lifecycle 125 Interruptions 126 Uncaught Exceptions 128 Thread Management 130 Definition and Start 130 Retention 132 Summary 137 Chapter聽8.聽HandlerThread: A High-Level Queueing Mechanism 139 Fundamentals 139 Lifecycle 141 Use Cases 142 Repeated Task Execution 143 Related Tasks 143 Task Chaining 146 Conditional Task Insertion 149 Summary 149 Chapter聽9.聽Control over Thread Execution Through the Executor Framework 151 Executor 151 Thread Pools 154 Predefined Thread Pools 154 Custom Thread Pools 155 Designing a Thread Pool 156 Lifecycle 160 Shutting Down the Thread Pool 161 Thread Pool Uses Cases and Pitfalls 163 Task Management 164 Task Representation 164 Submitting Tasks 165 Rejecting Tasks 169 ExecutorCompletionService 170 Summary 172 Chapter聽10.聽Tying a Background Task to the UI Thread with AsyncTask 175 Fundamentals 175 Creation and Start 178 Cancellation 179 States 180 Implementing the AsyncTask 181 Example: Downloading Images 182 Background Task Execution 185 Application Global Execution 187 Execution Across Platform Versions 188 Custom Execution 190 AsyncTask Alternatives 191 When an AsyncTask Is Trivially Implemented 191 Background Tasks That Need a Looper 192 Local Service 192 Using execute(Runnable) 192 Summary 193 Chapter聽11.聽Services 195 Why Use a Service for Asynchronous Execution? 195 Local, Remote, and Global Services 197 Creation and Execution 199 Lifecycle 199 Started Service 201 Implementing onStartCommand 202 Options for Restarting 202 User-Controlled Service 204 Task-Controlled Service 208 Bound Service 210 Local Binding 212 Choosing an Asynchronous Technique 215 Summary 216 Chapter聽12.聽IntentService 217 Fundamentals 217 Good Ways to Use an IntentService 219 Sequentially Ordered Tasks 219 Asynchronous Execution in BroadcastReceiver 222 IntentService Versus Service 225 Summary 225 Chapter聽13.聽Access ContentProviders with AsyncQueryHandler 227 Brief Introduction to ContentProvider 227 Justification for Background Processing of a ContentProvider 229 Using the AsyncQueryHandler 230 Example: Expanding Contact List 232 Understanding the AsyncQueryHandler 235 Limitations 236 Summary 236 Chapter聽14.聽Automatic Background Execution with Loaders 237 Loader Framework 238 LoaderManager 239 LoaderCallbacks 242 AsyncTaskLoader 243 Painless Data Loading with CursorLoader 244 Using the CursorLoader 245 Example: Contact list 245 Adding CRUD Support 247 Implementing Custom Loaders 251 Loader Lifecycle 251 Background Loading 252 Content Management 254 Delivering Cached Results 255 Example: Custom File Loader 256 Handling Multiple Loaders 259 Summary 260 Chapter聽15.聽Summary: Selecting an Asynchronous Technique 261 Keep It Simple 262 Thread and Resource Management 262 Message Communication for Responsiveness 263 Avoid Unexpected Task Termination 264 Easy Access to ContentProviders 265 Appendix聽A.聽Bibliography 267 Books 267 Articles 267 Presentations 267 Index 269 About the Author 279 Multithreading is essential if you want to create an Android app with a great user experience, but how do you know which techniques can help solve your problem? This practical book describes many asynchronous mechanisms available in the Android SDK, and provides guidelines for selecting the ones most appropriate for the app you’re building.Author Anders Goransson demonstrates the advantages and disadvantages of each technique, with sample code and detailed explanations for using it efficiently. The first part of the book describes the building blocks of asynchronous processing, and the second part covers Android libraries and constructs for developing fast, responsive, and well-structured apps.Understand multithreading basics in Java and on the Android platformLearn how threads communicate within and between processesUse strategies to reduce the risk of memory leaksManage the lifecycle of a basic threadRun tasks sequentially in the background with HandlerThreadUse Java’s Executor Framework to control or cancel threadsHandle background task execution with AsyncTask and IntentServiceAccess content providers with AsyncQueryHandlerUse loaders to update the UI with new data Annotation Multithreading is essential if you want to create an Android app with a great user experience, but how do you know which techniques can help solve your problem? This practical book describes many asynchronous mechanisms available in the Android SDK, and provides guidelines for selecting the ones most appropriate for the app youre building. Author Anders Goransson demonstrates the advantages and disadvantages of each technique, with sample code and detailed explanations for using it efficiently. The first part of the book describes the building blocks of asynchronous processing, and the second part covers Android libraries and constructs for developing fast, responsive, and well-structured apps. Understand multithreading basics in Java and on the Android platformLearn how threads communicate within and between processesUse strategies to reduce the risk of memory leaksManage the lifecycle of a basic threadRun tasks sequentially in the background with HandlerThreadUse Javas Executor Framework to control or cancel threadsHandle background task execution with AsyncTask and IntentServiceAccess content providers with AsyncQueryHandlerUse loaders to update the UI with new data