- 浏览: 169861 次
- 性别:
- 来自: 北京
-
最新评论
-
wangzhengfu:
看你这文章很纠结,既然把图贴出来了,为啥不贴上代码呢
android EditText 去除边框 -
sovio:
...
android EditText 去除边框 -
kdac:
啥J8翻译啊,谷歌翻译贴出来的吧?翻译不了就放原文,不伦不类, ...
android 弹出软键盘将底部视图顶起问题 -
shiny_txdd:
17:34:47,806 ERROR [ContextLoad ...
tomcat项目转jboss5.0 -
lenomon:
这里有篇实现无下划线的,Android使用TextView实现 ...
Android TextView中文字设置超链接、颜色、字体
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 onPreExecute
, doInBackground
,
onProgressUpdate
and onPostExecute
.
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:
-
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 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:
-
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. -
doInBackground(Params...)
, invoked on the background thread immediately afteronPreExecute()
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 usepublishProgress(Progress...)
to publish one or more units of progress. These values are published on the UI thread, in theonProgressUpdate(Progress...)
step. -
onProgressUpdate(Progress...)
, invoked on the UI thread after a call topublishProgress(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. -
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.
Cancelling a task
A task can be cancelled at any time by invoking cancel(boolean)
. Invoking
this method will cause subsequent calls to isCancelled()
to return true.
After invoking this method, onCancelled(Object)
, instead of
onPostExecute(Object)
will be invoked after doInBackground(Object[])
returns. To ensure that a task is cancelled as quickly as possible, you should always
check the return value of isCancelled()
periodically from
doInBackground(Object[])
, if possible (inside a loop for instance.)
Threading rules
There are a few threading rules that must be followed for this class to work properly:
- The task instance must be created on the UI thread.
-
execute(Params...)
must be invoked on the UI thread. - Do not call
onPreExecute()
,onPostExecute(Result)
,doInBackground(Params...)
,onProgressUpdate(Progress...)
manually. - The task can be executed only once (an exception will be thrown if a second execution is attempted.)
Memory observability
AsyncTask guarantees that all callback calls are synchronized in such a way that the following operations are safe without explicit synchronizations.
- Set member fields in the constructor or
onPreExecute()
, and refer to them indoInBackground(Params...)
. - Set member fields in
doInBackground(Params...)
, and refer to them inonProgressUpdate(Progress...)
andonPostExecute(Result)
.
发表评论
-
Android中canvas和paint的关系及使用
2011-11-30 10:15 2252刚刚开始接触学习Android的同鞋们在看到工程中出现的那么 ... -
Android 模仿renren的左右划动菜单栏
2011-11-10 13:47 2561模仿renren的左右划动菜单栏,主要通过Horizo ... -
android 抓包
2011-07-12 10:28 39881、下载TcpDump for Android ,或者本地下 ... -
Android参数设置父布局集体宽高
2011-06-21 11:47 3445LinearLayout gridContainer = (L ... -
Android应用按返回键完全退出应用
2011-06-07 17:58 7234很多网友可能发现自己 ... -
GIT和repo使用方法,下载android-2.6.29内核
2011-06-07 14:45 2003http://hi.baidu.com/kkernel/ ... -
动态增加TableLayout的行
2011-06-02 15:43 2551Just like HTML Tables on webpag ... -
如何申请 android google map API key
2011-06-01 15:16 16291.首先要得到你的debug keystore位置: 打 ... -
Android TextView中文字设置超链接、颜色、字体
2011-05-27 12:27 5394TextView是用来显示文本的,有时需要给TextView中 ... -
设置ProgressBar的颜色
2011-05-26 10:05 2265在《Android/OPhone开发完全讲义》 ... -
Android 调用系统的照相,浏览图片,转存并裁剪!
2011-05-25 12:59 4610public class AddCardActivity ex ... -
修改TabHost默认样式
2011-05-10 13:52 3205TabHost是Android提供的一个容器组件, ... -
Android控件美化Shape
2011-05-04 09:59 1216当然除了使用drawable这样的图片外今天谈下自定义图形 ... -
android开发中WebView的使用(附完整程序)
2011-05-03 14:00 2121WebView是个好东西,作用相当于一个迷你的浏览器,采用We ... -
android 弹出软键盘将底部视图顶起问题
2011-04-19 13:20 6985今天要做一个搜索功能,搜索界面采用AutoCompleteTe ... -
自定义ListView行间的分割线
2011-04-18 10:18 1614在Android平台中系统控件提供了灵活的自定义选项,所有 ... -
android xml中应用占位符
2011-04-02 18:24 1576Formatting and Styling Here ar ... -
android Toast大全(五种情形)建立属于你自己的Toast
2011-04-02 18:18 939Toast用于向用户显示一些帮助/提示。下面我做了5中效果,来 ... -
JadEclipse工具的使用
2011-04-01 11:49 2574JadEclipse工具的使用 1、下载工具 JadEcl ... -
Android 反编译apk 到java源码的方法
2011-04-01 11:20 975Android由于其代码是放在dalvik虚拟机上的托管代码, ...
评论