the asynctask framework: key methods · for use with a simple runnable object boolean cancel...
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/1.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/2.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/3.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/4.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/5.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/6.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/7.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/8.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/9.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/10.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/11.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/12.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/13.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/14.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/15.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/16.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/17.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/18.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/19.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/20.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/21.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/22.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/23.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/24.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/25.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/26.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/27.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/28.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/29.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/30.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/31.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/32.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/33.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/34.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/35.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/36.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/37.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/38.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/39.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/40.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/41.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/42.jpg)
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()](https://reader033.vdocuments.mx/reader033/viewer/2022043005/5f8c37dc6566ea4ff8041c1d/html5/thumbnails/43.jpg)
43
End of the AsyncTaskFramework: Key Methods