Efficient Android Threading: Asynchronous Processing Techniques for Android Applications

Efficient Android Threading: Asynchronous Processing Techniques for Android Applications

Anders Goransson

Language: English

Pages: 280

ISBN: 1449364136

Format: PDF / Kindle (mobi) / ePub

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


















other process. Enables interprocess message communication, as described in “Two-Way Communication” on page 86. callback Runna ble Task to execute on a thread. This is an internal instance field that holds the Runnable object from the Handler.post methods in “Handler” on page 60. Task message The task is represented by a java.lang.Runnable object to be executed on the consumer thread. Task messages cannot contain any data beyond the task itself. A MessageQueue can contain any combination of

use cases for batched submissions: invokeAll and invokeAny. Individual submission The most fundamental way to add a task to the thread pool is to build on its imple‐ mentation of Executor and call the execute method: ExecutorService executor = Executors.newSingleThreadExecutor(); executor.execute(new Runnable() { public void run() { doLongRunningOperation(); } }); The Executor interface can handle only Runnable tasks, but the ExecutorService extension contains more general methods allowing

without blocking. Example 9-5 utilizes invokeAll to execute two independent tasks concurrently on worker threads and combine the results when both have finished. It is typically used for retrieving network data from two different locations, where the results are mashed to‐ gether before being used. The data retrieval is initiated from a button click in an Activity—i.e., on the UI thread —but because invokeAll is a blocking call, it is executed from a SimpleExecutor (Example 9-1). Example 9-5.

4. Callback on a background thread: doInBackground. This executes the long-running task. 5. Report progress updates from the publishProgress method on the background thread. These trigger the onProgressUpdate callback on the UI thread, which typ‐ Fundamentals | 159 ically handles the update by changing a progress indicator on the screen. The pro‐ gress is defined by the Progress parameter. 6. The background execution is done and is followed by running a callback on the UI thread to report the

that may reference the Con text, typically an Activity with a view hierarchy. But you should avoid referencing the view hierarchy so that the views won’t be kept in memory when they are not needed. Hence, the AsyncTask should be declared as a static inner class that holds Implementing the AsyncTask | 163 a reference to the associated Context. The reference is removed by setting it to null when it is not needed anymore. Cancellation policy Allow tasks to be interrupted and cancelled, as

Download sample