public abstract class UserTask<Progress> extends TaskRunner
UserTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
A user task is defined by a computation that runs on a background thread and
whose result is published on the UI thread. A user task is defined by 3 generic
types, called Params, Progress and Result,
and 4 steps, called begin, doInBackground,
processProgress and end.
UserTask must be subclassed to be used. The subclass will override at least
one method (BackgroundTask.doInBackground()), and most often will override a
second one (BackgroundTask.onPostExecute(Boolean).)
Here is an example of subclassing:
private class DownloadFilesTask extends UserTask<URL, Integer, Long> {
public File doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
}
}
public void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
public void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
Once created, a task is executed very simply:
new DownloadFilesTask().execute(new URL[] { ... });
The three types used by a user task are the following:
Params, the type of the parameters sent to the task upon
execution.Progress, the type of the progress units published during
the background computation.Result, the type of the result of the background
computation.Not all types are always used by a user task. To mark a type as unused,
simply use the type Void:
private class MyTask extends UserTask<Void, Void, Void> { ... }
When a user task is executed, the task goes through 4 steps:
BackgroundTask.onPreExecute(), invoked on the UI thread immediately after the task
is executed. This step is normally used to setup the task, for instance by
showing a progress bar in the user interface.BackgroundTask.doInBackground(), invoked on the background thread
immediately after () finishes executing. This step is used
to perform background computation that can take a long time. The parameters
of the user task are passed to this step. The result of the computation must
be returned by this step and will be passed back to the last step. This step
can also use publishProgress(Object[]) to publish one or more units
of progress. These values are published on the UI thread, in the
onProgressUpdate(Object[]) step.(Object[]), invoked on the UI thread after a
call to publishProgress(Object[]). The timing of the execution is
undefined. This method is used to display any form of progress in the user
interface while the background computation is still executing. For instance,
it can be used to animate a progress bar or show logs in a text field.BackgroundTask.onPostExecute(Boolean), invoked on the UI thread after the background
computation finishes. The result of the background computation is passed to
this step as a parameter.There are a few threading rules that must be followed for this class to work properly:
TaskRunner.execute(BackgroundTask) must be invoked on the UI thread.(), BackgroundTask.onPostExecute(Boolean),
BackgroundTask.doInBackground(), (Object[])
manually.| Constructor and Description |
|---|
UserTask(TaskManager taskManager,
InternalHandler internalHandler)
Creates a new user task.
|
| Modifier and Type | Method and Description |
|---|---|
void |
onProgressUpdate(Object[] values)
Runs on the UI thread after
publishProgress(Object[]) is invoked. |
cancel, execute, getStatus, isCancelledwaitForTaskpublic UserTask(TaskManager taskManager, InternalHandler internalHandler)
public void onProgressUpdate(Object[] values)
publishProgress(Object[]) is invoked.
The specified values are the values passed to publishProgress(Object[]).values - The values indicating progress.publishProgress(Object[]),
BackgroundTask.doInBackground()This documentation is licensed by Andrew Bowley under the GPLv3 License.