+ All Categories
Home > Documents > The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel...

The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel...

Date post: 03-Aug-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
43
The AsyncTask Framework: Key Methods Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA
Transcript
Page 1: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

The AsyncTask Framework:

Key Methods

Douglas C. [email protected]

www.dre.vanderbilt.edu/~schmidt

Institute for Software

Integrated Systems

Vanderbilt University

Nashville, Tennessee, USA

Page 2: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

2

Learning Objectives in this Part of the Lesson• Recognize the capabilities provided by the Android AsyncTask framework

• Know which methods are provided by AsyncTask class

AsyncTaskHandler

3. execute(future)

2. onPreExecute()

4. doInBackGround()

FutureTask

Executor

UI Thread(main thread)

Lo

op

er

Message

Message

Message

Message

Message

Message

Queue

Message

5. onProgressUpdate()

6. onPostExecute()

1. execute(url)

Page 3: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

3

Categories of Methods in AsyncTask

Page 4: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

4

Categories of Methods in the AsyncTask Class• The AsyncTask class has two

types of methods

See developer.android.com/reference/android/os/AsyncTask.html

Page 5: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

5

• The AsyncTask class has two types of methods

• Public methods

• Typically invoked by clients

AsyncTask<Params, Progress, Result>

execute(Params... params)

• Execute task with specified parameters

AsyncTask<Params, Progress, Result>

executeOnExecutor(Executor exec,

Params... params)

• Execute task with specified parameters on specified Executor

static void execute(Runnable

runnable)

• Convenience version of execute(Object) for use with a simple Runnable object

boolean cancel

(boolean mayInterruptIfRunning)

• Attempts to cancel execution of this task

boolean isCancelled()

• True if task was cancelled before completing

Categories of Methods in the AsyncTask Class

Page 6: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

6

• The AsyncTask class has two types of methods

• Public methods

• Typically invoked by clients

AsyncTask<Params, Progress, Result>

execute(Params... params)

• Execute task with specified parameters

AsyncTask<Params, Progress, Result>

executeOnExecutor(Executor exec,

Params... params)

• Execute task with specified parameters on specified Executor

static void execute(Runnable

runnable)

• Convenience version of execute(Object) for use with a simple Runnable object

boolean cancel

(boolean mayInterruptIfRunning)

• Attempts to cancel execution of this task

boolean isCancelled()

• True if task was cancelled before completing

Categories of Methods in the AsyncTask Class

Run each async task one-at-a-time (serially) in a background

thread within a process

Page 7: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

7

• The AsyncTask class has two types of methods

• Public methods

• Typically invoked by clients

AsyncTask<Params, Progress, Result>

execute(Params... params)

• Execute task with specified parameters

AsyncTask<Params, Progress, Result>

executeOnExecutor(Executor exec,

Params... params)

• Execute task with specified parameters on specified Executor

static void execute(Runnable

runnable)

• Convenience version of execute(Object) for use with a simple Runnable object

boolean cancel

(boolean mayInterruptIfRunning)

• Attempts to cancel execution of this task

boolean isCancelled()

• True if task was cancelled before completing

Categories of Methods in the AsyncTask Class

Runs multiple async tasks concurrently in a pool of threads within a process

Page 8: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

8

• The AsyncTask class has two types of methods

• Public methods

• Typically invoked by clients

AsyncTask<Params, Progress, Result>

execute(Params... params)

• Execute task with specified parameters

AsyncTask<Params, Progress, Result>

executeOnExecutor(Executor exec,

Params... params)

• Execute task with specified parameters on specified Executor

static void execute(Runnable

runnable)

• Convenience version of execute(Object) for use with a simple Runnable object

boolean cancel

(boolean mayInterruptIfRunning)

• Attempts to cancel execution of this task

boolean isCancelled()

• True if task was cancelled before completing

Categories of Methods in the AsyncTask Class

A simple front-end to the underlying executor

Page 9: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

9

• The AsyncTask class has two types of methods

• Public methods

• Typically invoked by clients

AsyncTask<Params, Progress, Result>

execute(Params... params)

• Execute task with specified parameters

AsyncTask<Params, Progress, Result>

executeOnExecutor(Executor exec,

Params... params)

• Execute task with specified parameters on specified Executor

static void execute(Runnable

runnable)

• Convenience version of execute(Object) for use with a simple Runnable object

boolean cancel

(boolean mayInterruptIfRunning)

• Attempts to cancel execution of this task

boolean isCancelled()

• True if task was cancelled before completing

Categories of Methods in the AsyncTask Class

See earlier lessons on “Managing the Thread Lifecycle”

Requires cooperation by the async task, i.e., it’s voluntary

Page 10: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

10

• The AsyncTask class has two types of methods

• Public methods

• Typically invoked by clients

AsyncTask<Params, Progress, Result>

execute(Params... params)

• Execute task with specified parameters

AsyncTask<Params, Progress, Result>

executeOnExecutor(Executor exec,

Params... params)

• Execute task with specified parameters on specified Executor

static void execute(Runnable

runnable)

• Convenience version of execute(Object) for use with a simple Runnable object

boolean cancel

(boolean mayInterruptIfRunning)

• Attempts to cancel execution of this task

boolean isCancelled()

• True if task was cancelled before completing

Categories of Methods in the AsyncTask Class

Called in doInBackground() to check if task should shutdown

Page 11: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

11

• The AsyncTask class has two types of methods

• Public methods

• Protected methods

• Overridden by subclasses

void onPreExecute()

• Runs on UI thread before doInBackground()

abstract Result doInBackground

(Params... params)

• Override this method to perform a computation in a background thread

void onProgressUpdate(Progress...

values)

• Runs on UI thread after publishProgress() called

void onPostExecute(Result result)

• Runs on UI thread after doInBackground()

void onCancelled(Result result)

• Runs on UI thread after cancel() is invoked & doInBackground() has finished

...

Categories of Methods in the AsyncTask Class

Page 12: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

12

• The AsyncTask class has two types of methods

• Public methods

• Protected methods

• Overridden by subclasses

Categories of Methods in the AsyncTask Class

AsyncTaskHandler

3. execute(future)

2. onPreExecute()

4. doInBackGround()

FutureTask

Executor

5. onProgressUpdate()

6. onPostExecute()

1. execute(url)

UI Thread(main thread)

The AsyncTask framework applies the Template Method pattern to call these methods at different points of time & in different thread contexts

See en.wikipedia.org/wiki/Template_method_pattern

Page 13: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

13

• The AsyncTask class has two types of methods

• Public methods

• Protected methods

• Overridden by subclasses

void onPreExecute()

• Runs on UI thread before doInBackground()

abstract Result doInBackground

(Params... params)

• Override this method to perform a computation in a background thread

void onProgressUpdate(Progress...

values)

• Runs on UI thread after publishProgress() called

void onPostExecute(Result result)

• Runs on UI thread after doInBackground()

void onCancelled(Result result)

• Runs on UI thread after cancel() is invoked & doInBackground() has finished

...

Categories of Methods in the AsyncTask Class

Called in UI thread after execute() called, i.e.,

prior to other processing

Page 14: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

14

• The AsyncTask class has two types of methods

• Public methods

• Protected methods

• Overridden by subclasses

void onPreExecute()

• Runs on UI thread before doInBackground()

abstract Result doInBackground

(Params... params)

• Override this method to perform a computation in a background thread

void onProgressUpdate(Progress...

values)

• Runs on UI thread after publishProgress() called

void onPostExecute(Result result)

• Runs on UI thread after doInBackground()

void onCancelled(Result result)

• Runs on UI thread after cancel() is invoked & doInBackground() has finished

...

Categories of Methods in the AsyncTask Class

Runs in a background thread to perform the computation

Page 15: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

15

• The AsyncTask class has two types of methods

• Public methods

• Protected methods

• Overridden by subclasses

void onPreExecute()

• Runs on UI thread before doInBackground()

abstract Result doInBackground

(Params... params)

• Override this method to perform a computation in a background thread

void onProgressUpdate(Progress...

values)

• Runs on UI thread after publishProgress() called

void onPostExecute(Result result)

• Runs on UI thread after doInBackground()

void onCancelled(Result result)

• Runs on UI thread after cancel() is invoked & doInBackground() has finished

...

Categories of Methods in the AsyncTask Class

Called in UI thread to convey incremental results sent from

a background thread

Page 16: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

16

• The AsyncTask class has two types of methods

• Public methods

• Protected methods

• Overridden by subclasses

void onPreExecute()

• Runs on UI thread before doInBackground()

abstract Result doInBackground

(Params... params)

• Override this method to perform a computation in a background thread

void onProgressUpdate(Progress...

values)

• Runs on UI thread after publishProgress() called

void onPostExecute(Result result)

• Runs on UI thread after doInBackground()

void onCancelled(Result result)

• Runs on UI thread after cancel() is invoked & doInBackground() has finished

...

Categories of Methods in the AsyncTask Class

Called in UI thread after all background processing

is finished successfully

Page 17: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

17

• The AsyncTask class has two types of methods

• Public methods

• Protected methods

• Overridden by subclasses

void onPreExecute()

• Runs on UI thread before doInBackground()

abstract Result doInBackground

(Params... params)

• Override this method to perform a computation in a background thread

void onProgressUpdate(Progress...

values)

• Runs on UI thread after publishProgress() called

void onPostExecute(Result result)

• Runs on UI thread after doInBackground()

void onCancelled(Result result)

• Runs on UI thread after cancel() is invoked & doInBackground() has finished

...

Categories of Methods in the AsyncTask Class

Called in UI thread after background processing

has been cancelled

Page 18: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

18

• The AsyncTask class has two types of methods

• Public methods

• Protected methods

• Overridden by subclasses

• Final methods

void publishProgress(Progress...

values)

• Invoked from doInBackground() to publish updates on UI thread while the background computation is still running

...

Categories of Methods in the AsyncTask Class

Each call to this method triggers execution of onProgressUpdate() in the UI thread

Page 19: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

19

Overriding Hook Methods in the AsyncTask Class

Page 20: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

20

• AsyncTask must be extended& one or more of its hookmethods overridden

Overriding Hook Methods in the AsyncTask Class

ImageDownloadTask

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

AsyncTask

executeOnExecutor()

execute()

cancel()

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

Page 21: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

21

• AsyncTask must be extended& one or more of its hookmethods overridden

Overriding Hook Methods in the AsyncTask Class

ImageDownloadTask

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

AsyncTask

executeOnExecutor()

execute()

cancel()

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

The doInBackground() method must be overridden

Page 22: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

22

ImageDownloadTask

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

AsyncTask

executeOnExecutor()

execute()

cancel()

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

• AsyncTask must be extended& one or more of its hookmethods overridden

Can only be called once per async task object by

code in the UI thread

Overriding Hook Methods in the AsyncTask Class

Page 23: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

23

ImageDownloadTask

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

AsyncTask

executeOnExecutor()

execute()

cancel()

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

• AsyncTask must be extended& one or more of its hookmethods overridden

See en.wikipedia.org/wiki/Template_method_pattern

Implemented as a variant of the Template

Method pattern

Overriding Hook Methods in the AsyncTask Class

Page 24: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

24

ImageDownloadTask

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

AsyncTask

executeOnExecutor()

execute()

cancel()

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

• AsyncTask must be extended& one or more of its hookmethods overridden

Is passed an executor used to run multiple async task

objects concurrently

Overriding Hook Methods in the AsyncTask Class

Page 25: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

25

• AsyncTask must be extended& one or more of its hookmethods overridden

Overriding Hook Methods in the AsyncTask Class

ImageDownloadTask

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

AsyncTask

executeOnExecutor()

execute()

cancel()

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

Invoked by framework in the UI thread to perform

initialization actions

Page 26: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

26

• AsyncTask must be extended& one or more of its hookmethods overridden

See www.androiddesignpatterns.com/2014/01/thread-scheduling-in-android.html

Overriding Hook Methods in the AsyncTask Class

ImageDownloadTask

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

AsyncTask

executeOnExecutor()

execute()

cancel()

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

Invoked by framework in a to perform long duration operations at

the “background” thread priority

Page 27: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

27

• AsyncTask must be extended& one or more of its hookmethods overridden

Overriding Hook Methods in the AsyncTask Class

ImageDownloadTask

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

AsyncTask

executeOnExecutor()

execute()

cancel()

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

Invoked by framework in UI thread when background

thread calls publishProgress()

Page 28: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

28

ImageDownloadTask

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

AsyncTask

executeOnExecutor()

execute()

cancel()

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

• AsyncTask must be extended& one or more of its hookmethods overridden

Invoked by framework in UI thread when doInBackground() returns its result

Overriding Hook Methods in the AsyncTask Class

Page 29: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

29

ImageDownloadTask

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

AsyncTask

executeOnExecutor()

execute()

cancel()

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

• AsyncTask must be extended& one or more of its hookmethods overridden

Called by application to attempt to stop the execution of the task

Overriding Hook Methods in the AsyncTask Class

See upcoming lessons on “Managing the Thread Lifecycle”

Page 30: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

30

ImageDownloadTask

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

AsyncTask

executeOnExecutor()

execute()

cancel()

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

• AsyncTask must be extended& one or more of its hookmethods overridden

Invoked by framework in the UI thread after cancel() is called & doInBackground() is finished

If onCancelled() is called then onPostExecute() is not called & vice versa

Overriding Hook Methods in the AsyncTask Class

Page 31: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

31

ImageDownloadTask

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

AsyncTask

executeOnExecutor()

execute()

cancel()

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

• AsyncTask must be extended& one or more of its hookmethods overridden

Should periodically call isCancelled() to check to see if it’s been cancelled

Similar to using the Java interrupt() method to voluntarily shutdown threads

Overriding Hook Methods in the AsyncTask Class

Page 32: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

32

ImageDownloadTask

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

AsyncTask

executeOnExecutor()

execute()

cancel()

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

• AsyncTask is also parameterized with three types used by itshook methods

Params, Progress, Result

• Params – Type used in background work

• Progress – Type used when indicating progress

• Result – Type of result

Overriding Hook Methods in the AsyncTask Class

Page 33: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

33

ImageDownloadTask

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

AsyncTask

executeOnExecutor()

execute()

cancel()

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

• AsyncTask is also parameterized with three types used by itshook methods

Params, Progress, Result

• Params – Type used in background work

• Progress – Type used when indicating progress

• Result – Type of result

Overriding Hook Methods in the AsyncTask Class

Page 34: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

34

ImageDownloadTask

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

AsyncTask

executeOnExecutor()

execute()

cancel()

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

• AsyncTask is also parameterized with three types used by itshook methods

Params, Progress, Result

• Params – Type used in background work

• Progress – Type used when indicating progress

• Result – Type of result

Overriding Hook Methods in the AsyncTask Class

Page 35: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

35

ImageDownloadTask

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

AsyncTask

• AsyncTask is also parameterized with three types used by itshook methods

Params, Progress, Result

Overriding Hook Methods in the AsyncTask Class

executeOnExecutor()

execute()

cancel()

onPreExecute()

doInBackground()

onProgressUpdate()

onPostExecute()

onCancelled()

• Params – Type used in background work

• Progress – Type used when indicating progress

• Result – Type of result

Page 36: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

36

• Apps must customize theAsyncTask class to meet their concurrency needs

Overriding Hook Methods in the AsyncTask Class

Variant of developer.android.com/reference/android/os/AsyncTask.html

class DownloadTask extends

AsyncTask<Uri, Integer, Long> {

protected void onPreExecute()

{ startDialog("Downloading file"); }

protected Long doInBackground

(Uri... urls)

{ /* Download url & publish process */ }

protected void onProgressUpdate

(Integer... progress)

{ setProgressPercent(progress[0]); }

protected void onPostExecute(Long res)

{ stopDialog("Got " + res + " bytes"); }

}

new DownloadTask().execute(downloadURL);

Page 37: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

37

• Apps must customize theAsyncTask class to meet their concurrency needs

Overriding Hook Methods in the AsyncTask Class

Extend AsyncTask & fill in generic parameters

class DownloadTask extends

AsyncTask<Uri, Integer, Long> {

protected void onPreExecute()

{ startDialog("Downloading file"); }

protected Long doInBackground

(Uri... urls)

{ /* Download url & publish process */ }

protected void onProgressUpdate

(Integer... progress)

{ setProgressPercent(progress[0]); }

protected void onPostExecute(Long res)

{ stopDialog("Got " + res + " bytes"); }

}

new DownloadTask().execute(downloadURL);

Page 38: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

38

• Apps must customize theAsyncTask class to meet their concurrency needs

Overriding Hook Methods in the AsyncTask Class

This template method initiates async processing

class DownloadTask extends

AsyncTask<Uri, Integer, Long> {

protected void onPreExecute()

{ startDialog("Downloading file"); }

protected Long doInBackground

(Uri... urls)

{ /* Download url & publish process */ }

protected void onProgressUpdate

(Integer... progress)

{ setProgressPercent(progress[0]); }

protected void onPostExecute(Long res)

{ stopDialog("Got " + res + " bytes"); }

}

new DownloadTask().execute(downloadURL);

Page 39: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

39

• Apps must customize theAsyncTask class to meet their concurrency needs

Overriding Hook Methods in the AsyncTask Class

Hook method called by framework in UI thread

class DownloadTask extends

AsyncTask<Uri, Integer, Long> {

protected void onPreExecute()

{ startDialog("Downloading file"); }

protected Long doInBackground

(Uri... urls)

{ /* Download url & publish process */ }

protected void onProgressUpdate

(Integer... progress)

{ setProgressPercent(progress[0]); }

protected void onPostExecute(Long res)

{ stopDialog("Got " + res + " bytes"); }

}

new DownloadTask().execute(downloadURL);

Page 40: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

40

• Apps must customize theAsyncTask class to meet their concurrency needs

Overriding Hook Methods in the AsyncTask Class

Hook method called by framework in background

class DownloadTask extends

AsyncTask<Uri, Integer, Long> {

protected void onPreExecute()

{ startDialog("Downloading file"); }

protected Long doInBackground

(Uri... urls)

{ /* Download url & publish process */ }

protected void onProgressUpdate

(Integer... progress)

{ setProgressPercent(progress[0]); }

protected void onPostExecute(Long res)

{ stopDialog("Got " + res + " bytes"); }

}

new DownloadTask().execute(downloadURL);

Page 41: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

41

• Apps must customize theAsyncTask class to meet their concurrency needs

Overriding Hook Methods in the AsyncTask Class

Hook method called by framework in UI thread

class DownloadTask extends

AsyncTask<Uri, Integer, Long> {

protected void onPreExecute()

{ startDialog("Downloading file"); }

protected Long doInBackground

(Uri... urls)

{ /* Download url & publish process */ }

protected void onProgressUpdate

(Integer... progress)

{ setProgressPercent(progress[0]); }

protected void onPostExecute(Long res)

{ stopDialog("Got " + res + " bytes"); }

}

new DownloadTask().execute(downloadURL);

Page 42: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

42

• Apps must customize theAsyncTask class to meet their concurrency needs

Overriding Hook Methods in the AsyncTask Class

Hook method called by framework in UI thread

class DownloadTask extends

AsyncTask<Uri, Integer, Long> {

protected void onPreExecute()

{ startDialog("Downloading file"); }

protected Long doInBackground

(Uri... urls)

{ /* Download url & publish process */ }

protected void onProgressUpdate

(Integer... progress)

{ setProgressPercent(progress[0]); }

protected void onPostExecute(Long res)

{ stopDialog("Got " + res + " bytes"); }

}

new DownloadTask().execute(downloadURL);

Page 43: The AsyncTask Framework: Key Methods · for use with a simple Runnable object boolean cancel (boolean mayInterruptIfRunning) •Attempts to cancel execution of this task boolean isCancelled()

43

End of the AsyncTaskFramework: Key Methods


Recommended