public abstract class
AsyncTask
extends Object
java.lang.Object
↳ android.os.AsyncTask<Params, Progress, Result>
Class Overview
AsyncTask 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.
An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called begin, doInBackground, processProgress and end.
Usage
AsyncTask must be subclassed to be used. The subclass will override at least one method (doInBackground(Params...)), and most often will override a second one (onPostExecute(Result).)
Here is an example of subclassing:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long 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));
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
Once created, a task is executed very simply:
new DownloadFilesTask().execute(url1, url2, url3);
AsyncTask's generic types
The three types used by an asynchronous task are the following:
1.Params, the type of the parameters sent to the task upon execution.
2.Progress, the type of the progress units published during the background computation.
3.Result, the type of the result of the background computation.
Not all types are always used by an asynchronous task. To mark a type as unused, simply use the type Void:
private class MyTask extends AsyncTask<Void, Void, Void> { ... }
The 4 steps
When an asynchronous task is executed, the task goes through 4 steps:
1.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.
2.doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous 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(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.
3.onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). 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.
4.onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter
分享到:
相关推荐
AdvancedAsyncTask, 面向Android的增强程序库 AdvancedAsyncTask 概述这个库增强了使用 AsyncTask api的功能。特性你可以在任务队列( 从fifo队列到优先级队列) 中处理任务的优先级。你可以处理任务的线程运行优先级...
AsyncTask是一个很常用的API,尤其异步处理数据并将数据应用到视图的操作场合。其实AsyncTask并不是那么好,甚至有些糟糕。本文我会讲AsyncTask会引起哪些问题,如何修复这些问题,并且关于AsyncTask的一些替代方案...
MAD_InClassAssignment6:AsyncTask和API的用法
AsyncTask是一个很常用的API,尤其异步处理数据并将数据应用到视图的操作场合。其实AsyncTask并不是那么好,甚至有些糟糕。本文我会讲AsyncTask会引起哪些问题,如何修复这些问题,并且关于AsyncTask的一些替代方案...
此库已增强,可以使用AsyncTask api。 产品特点 您可以在任务队列(从fifo队列到优先级队列)中处理任务的优先级。 您可以处理任务运行线程的优先级。 您可以为任务设置计时器。 Gradle和Maven Gradle ...
从 Android Api >= AsyncTasks 是串行执行的,这意味着你不能在不调用 task.executeOnExecutor(...) 的情况下同时运行 2 个或更多 AsyncTasks,这并不像你想的那么容易。 AsyncTask-Executor 承担在任何 Android ...
此时就需要直接上Thread了,这里参考AsyncTask的API封装了一个ThreadTask,便于必要时代码替换,欢迎交流! 正文实例代码: import android.os.Handler; import android.os.HandlerThread; import andr
Asynctask作为Android的基础之一,怎么使用就不多讲解了,网上到处都是教程,建议查看Android官方API文档:https://developer.android.google.cn/reference/android/os/AsyncTask.html 这里只实现一个小Demo程序,供...
AsyncTask是一个很常用的API,尤其异步处理数据并将数据应用到视图的操作场合。其实AsyncTask并不是那么好,甚至有些糟糕。本文我会讲AsyncTask会引起哪些问题,如何修复这些问题,并且关于AsyncTask的一些替代方案...
Android API 3时引进了AsyncTask,也叫异步任务。使用它可以很方便的更新主线程中的UI,使用它比Handler、Thread更简单。由于AsyncTask是抽象类,要使用它首先要创建子类继承它。创建子类时需要三个泛型参数
AsyncTask (API level 3,所以几乎所有目前在市面上流通的 Android 版本皆可使用) 是除 Thread 外的另一种选择,Android 团队鼓励主执行绪(UI thread) 专注于操作 & 画面的流畅呈现, 其余工作 (如网络资料传输、...
最近学到用AsyncTask来处理有关网络的操作。虽然代码看上去不是很复杂,但仍有很多地方有疑惑。所以研读了一下API文档,在这里把我学到的和练习的代码展示出来。如有错误,欢迎指出! 一、关于AsyncTask的 在创建...
ChuckNorrisApi-AsyncTask-Example:示范uma api usando异步消费任务
图书清单图书清单应用程序允许用户通过输入与该书相关的关键字来搜索任何图书。 它使用AsyncTask查询Google Books API以获取结果并向用户显示相关信息。
我们都知道,Android UI...AsyncTask很早就出现在Android的API里了,所以我相信大多数朋友对它的用法都已经非常熟悉。不过今天我还是准备从AsyncTask的基本用法开始讲起,然后我们再来一起分析下AsyncTask源码,看看它
只是一个围绕 AsyncTask 的简单包装器,具有更简单的 API。 请参阅以下案例: new AsyncTask() { protected void onPreExecute() { } @Override protected String doInBackground(Void... params) {
android.os.AsyncTask,一个执行异步操作的类,我们可以使用它来处理后台任务,并且在UI线程中处理结果,而无需关心线程的问题。 AsyncTask 内部是使用 {@link Thread}和{@link Handler}来实现的。理想情况下,应将...
系统的AsyncTask在不同版本的API的并发策略是不同的,在Android4.0以上只能运行一个Asynctask。Multiasynctask可以同时更多的子线程做的事情。默认为5个,而线程池的线程个数是可以修改的! 使用方式如下,基本和...