Chapter  18

Exploring the AsyncTask

In Chapter 17, you saw the need for handlers and worker threads. These handlers helped you to run long-running tasks on a worker thread while the main thread kept the house in order.

Android SDK has recognized this as a pattern and abstracted all of the handler and thread details into a utility class called AsyncTask. You can use AsyncTask to run tasks that take longer than five seconds in the context of UI. (We will cover how to run really long-running tasks, ranging from minutes to even hours, through services in Chapter 19.)

Listing 18–1 shows a high-level pseudo code of using an AsyncTask from a menu handler.

Listing 18–1. Usage Pattern for an AsyncTask by an Activity

public class MyActivity {     //menu ...

Get Pro Android 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.