Android Developer Fundamentals
Background
Tasks
MT Chapter 4 Async Task
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 1
4.0 International License
AsyncTask
&
AsyncTaskLoader
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 2
4.0 International License
Contents
● Threads
● AsyncTask
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 3
4.0 International License
Threads
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 4
4.0 International License
The main thread
● Independent path of execution in a running program
● Code is executed line by line
● App runs on Java thread called "main" or "UI thread"
● Draws UI on the screen
● Responds to user actions by handling UI events
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 5
4.0 International License
The Main thread must be fast
● Hardware updates screen every 16 milliseconds
● UI thread has 16 ms to do all its work
● If it takes too long, app stutters or hangs
WAI
T
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 6
4.0 International License
Users uninstall unresponsive apps
● If the UI waits too long for an
operation to finish, it becomes
unresponsive
● The framework shows an
Application Not Responding
(ANR) dialog
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 7
4.0 International License
What is a long running task?
● Network operations
● Long calculations
● Downloading/uploading files
● Processing images
● Loading data
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 8
4.0 International License
Background threads
Execute long running tasks on a background thread
Main Thread (UI Thread)
Update UI
● AsyncTask
● The Loader Framework
● Services
Worker Thread Do some work
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 9
4.0 International License
Two rules for Android threads
● Do not block the UI thread
○ Complete all work in less than 16 ms for each screen
○ Run slow non-UI work on a non-UI thread
● Do not access the Android UI toolkit from outside
the UI thread
○ Do UI work only on the UI thread
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 10
4.0 International License
AsyncTask
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 11
4.0 International License
What is AsyncTask?
Use AsyncTask to implement basic background tasks
Main Thread (UI Thread)
onPostExecute()
Worker Thread
doInBackground()
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 12
4.0 International License
Override two methods
● doInBackground()—runs on a background thread
○ All the work to happen in the background
● onPostExecute()—runs on main thread when work done
○ Process results
○ Publish results to the UI
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 13
4.0 International License
AsyncTask helper methods
● onPreExecute()
○ Runs on the main thread
○ Sets up the task
● onProgressUpdate()
○ Runs on the main thread
○ receives calls from publishProgress() from background thread
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 14
4.0 International License
AsyncTask helper methods
Main Thread (UI Thread)
onPreExecute() onProgressUpdate() onPostExecute()
Worker Thread publishProgress()
doInBackground()
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 15
4.0 International License
Creating an AsyncTask
[Link] AsyncTask
[Link] data type sent to doInBackground()
[Link] data type of progress units for
onProgressUpdate()
[Link] data type of result for onPostExecute()
private class MyAsyncTask
extends AsyncTask<URL, Integer, Bitmap> {...}
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 16
4.0 International License
MyAsyncTask class definition
private class MyAsyncTask
extends AsyncTask<String, Integer, Bitmap> {...}
doInBackground()
doInBackground()
onProgressUpdate()
doInBackground(
onPostExecute()
● String—could be query, URI for filename
● Integer—percentage completed, steps done
● Bitmap—an image to be displayed
● Use Void if no data passed
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 17
4.0 International License
onPreExecute()
protected void onPreExecute() {
// display a progress bar
// show a toast
}
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 18
4.0 International License
doInBackground()
protected Bitmap doInBackground(String... query) {
// Get the bitmap
return bitmap;
}
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 19
4.0 International License
onProgressUpdate()
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 20
4.0 International License
onPostExecute()
protected void onPostExecute(Bitmap result) {
// Do something with the bitmap
}
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 21
4.0 International License
Start background work
public void loadImage (View view) {
String query = [Link]().toString();
new MyAsyncTask(query).execute();
}
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 22
4.0 International License
Limitations of AsyncTask
● When device configuration changes, Activity is destroyed
● AsyncTask cannot connect to Activity anymore
● New AsyncTask created for every config change
● Old AsyncTasks stay around
● App may run out of memory or crash
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 23
4.0 International License
When to use AsyncTask
● Short or interruptible tasks
● Tasks that do not need to report back to UI or user
● Lower priority tasks that can be left unfinished
● Use AsyncTaskLoader otherwise
This work is licensed under a Creative
Android Developer Fundamentals AsyncTask Commons Attribution-NonCommercial 24
4.0 International License
END
Android Developer Fundamentals 25