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

Parsing the Turing Test: Philosophical and Methodological Issues in the Quest for the Thinking Computer

High Performance MySQL (2nd Edition)

Office 2016 Simplified

CMS Security Handbook

Computing Meaning, Volume 4 (Text, Speech and Language Technology, Volume 47)

Oracle Fusion Middleware 11g Architecture and Management (1st Edition)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

like this: $ adb shell ps | grep com.eat USER PID PPID VSIZE RSS WCHAN PS NAME u0_a72 4257 144 320304 34540 ffffffff 00000000 S com.eat From this output, we can extract the following interesting properties of the com.eat application: • UID: u0_a72 • PID: 4257 • PPID: 144 (process number of the parent, which in the case of an Android appli‐ cation is always the Zygote) 1. I have used the string EAT to create a namespace for applications in this book. The string is an acronym of the book’s title.

number of concurrent tasks. Thread pools (Chapter 9) or HandlerThread (Chapter 8) offer constraints on the num‐ ber of executing threads. Retention A thread does not follow the lifecycle of an Android component that has started it or its underlying objects (see “The lifecycle mismatch” on page 95). Once a thread is started, it will execute until either its run method finishes or the whole application process terminates. Therefore, the thread lifetime can outlive the component lifetime. When the

change (e.g., a thread) and passed to the new Activity object. public Object getLastNonConfigurationInstance() Called in the new Activity object to retrieve the retained object returned in onRe tainNonConfigurationInstance() after a configuration change has been made. It can be called in onCreate or onStart and returns null if the Activity is started for another reason than a configuration change. As the ThreadRetainActivity listing shows, an alive thread can be passed across Ac tivity objects

that back‐ ground tasks get delayed, or worse still, not executed at all. Because all AsyncTask instances share this global execution, they all can have an impact on each other, which depends on the execution environment: Sequential execution (SERIAL_EXECUTOR) Tasks that are executed sequentially will not be processed on a worker thread until all the preceding tasks in the application have been processed. This applies to any tasks launched through executeOnExecutor(AsyncTask.SERIAL_EXECUTOR) or

executeTaskSequentially() { 172 | Chapter 10: Tying a Background Task to the UI Thread with AsyncTask new MyActivityAsyncTask().executeOnExecutor( ((EatApplication)getApplication).getCustomSequentialExecutor()); } public class MyService extends Service { private void executeTaskSequentially() { new MyServiceAsyncTask().executeOnExecutor( ((EatApplication)getApplication).getCustomSequentialExecutor()); } AsyncTask Alternatives Due to its simplicity, the AsyncTask is a popular asynchronous

Download sample

Download