AsyncTask
2016-02-23 18:10
302 查看
AsyncTask
AsyncTask的设计目标是比较短的操作(最多几秒),如果希望执行更长时间,使用 java.util.concurrent 中的 Executor, ThreadPoolExecutor, FutureTask更合适。Asyntask的泛型实现:
Params:发送给Asyntask的执行参数类型
Progress:the type of the progress units published during the background computation
Result: background计算阶段的结果类型
Asyntask执行中的四步:
onPreExecute() : 任务执行前执行,一般用途例如显示加载框,进度条
doInBackground(Params...) : 执行后台任务(途中可更新进度条)
onProgressUpdate(Progress...) : 更新进度条
onPostExecute(Result) :
注意点
Asyntask实例必须在UI线程中简历execute(Params...)必须在UI线程中调用。
new DownloadFilesTask().execute(urlStr);
不要手动调用onPreExecute(), onPostExecute()..等方法
The task can be executed only once(an exception will be thrown if a second execution is attempted)
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 in doInBackground(Params...)
Set member fields in doInBackground(Params...), and refer to them in onProgressUpdate(Progress...) and onPostExecute(Result)
相关文章推荐
- 面向对象之包
- ADB命令小结
- jquery时间倒计时
- 运行独立python 引用其他目录包
- Eclipse 搭建Struts2
- Spark 资源池简介
- js验证textarea里面是否有换行符
- sql替换字符
- 其它
- Android Service与IntentService区别
- java动态控制线程的启动和停止
- JBoss Web和 Tomcat的区别
- 用原生JS实现一个简单的计时器demo
- 类变量和实例变量的区别
- 生命的尊严
- WPF圆形进度条制作
- iOS 几种常见的数据储存方式
- Lua String 的扩展函数
- iOS小明开发笔记(十九) (Mac 终端命令介绍)
- POJ 3831 & HDU 3264 Open-air shopping malls(几何)