AsyncTask官方教程-推荐用AsyncTask少用Thread
Using AsyncTask
AsyncTask
allows you to perform asynchronous work on your user interface. It performs the blocking operations in a worker thread and then publishes the results on the UI thread, without requiring you to handle threads and/or handlers yourself.
To use it, you must subclass AsyncTask
and implement the doInBackground()
callback method, which runs in a pool of background threads. To update your UI, you should implement onPostExecute()
, which delivers the result from doInBackground()
and runs in the UI thread, so you can safely update your UI. You can then run the task by calling execute()
from the UI thread.
For example, you can implement the previous example using AsyncTask
this way:
public void onClick(View v) {
new DownloadImageTask().execute("http://example.com/image.png");
} private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
/** The system calls this to perform work in a worker thread and
* delivers it the parameters given to AsyncTask.execute() */
protected Bitmap doInBackground(String... urls) {
return loadImageFromNetwork(urls[0]);
} /** The system calls this to perform work in the UI thread and delivers
* the result from doInBackground() */
protected void onPostExecute(Bitmap result) {
mImageView.setImageBitmap(result);
}
}
Now the UI is safe and the code is simpler, because it separates the work into the part that should be done on a worker thread and the part that should be done on the UI thread.
You should read the AsyncTask
reference for a full understanding on how to use this class, but here is a quick overview of how it works:
- You can specify the type of the parameters, the progress values, and the final value of the task, using generics
- The method
doInBackground()
executes automatically on a worker thread onPreExecute()
,onPostExecute()
, andonProgressUpdate()
are all invoked on the UI thread- The value returned by
doInBackground()
is sent toonPostExecute()
- You can call
publishProgress()
at anytime indoInBackground()
to executeonProgressUpdate()
on the UI thread - You can cancel the task at any time, from any thread
Caution: Another problem you might encounter when using a worker thread is unexpected restarts in your activity due to a runtime configuration change (such as when the user changes the screen orientation), which may destroy your worker thread. To see how you can persist your task during one of these restarts and how to properly cancel the task when the activity is destroyed, see the source code for the Shelves sample application.
AsyncTask官方教程-推荐用AsyncTask少用Thread的更多相关文章
- DroidParts 中文系列教程(基于官方教程)
DroidParts中文系列教程(基于官方教程) (一)DroidParts框架概况 2014年4月18日星期五 11:36 他是一个精心构造的安卓框架,包括下面这些基本功能 DI依赖注入,可以注入V ...
- Ceisum官方教程2 -- 项目实例(workshop)
原文地址:https://cesiumjs.org/tutorials/Cesium-Workshop/ 概述 我们很高兴欢迎你加入Cesium社区!为了让你能基于Cesium开发自己的3d 地图项目 ...
- Unity性能优化(3)-官方教程Optimizing garbage collection in Unity games翻译
本文是Unity官方教程,性能优化系列的第三篇<Optimizing garbage collection in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...
- Unity性能优化(4)-官方教程Optimizing graphics rendering in Unity games翻译
本文是Unity官方教程,性能优化系列的第四篇<Optimizing graphics rendering in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...
- Unity性能优化(2)-官方教程Diagnosing performance problems using the Profiler window翻译
本文是Unity官方教程,性能优化系列的第二篇<Diagnosing performance problems using the Profiler window>的简单翻译. 相关文章: ...
- 详解 “Android UI”设计官方教程
我们曾经给大家一个<MeeGo移动终端设备开发UI设计基础教程>,同时很多朋友都在寻找Android UI开发的教程,我们从Android的官方开发者博客找了一份幻灯片,介绍了一些Andr ...
- Asp.Net MVC4.0 官方教程 入门指南之三--添加一个视图
Asp.Net MVC4.0 官方教程 入门指南之三--添加一个视图 在本节中,您需要修改HelloWorldController类,从而使用视图模板文件,干净优雅的封装生成返回到客户端浏览器HTML ...
- Spring 官方教程:使用 Restdocs 创建 API 文档
https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=2247483998&idx=1&sn=6ae5fa795d36b1 ...
- Ubuntu 镜像制作 官方教程
rufus工具下载:下载链接 官方教程:官方教程链接 软件界面预览: 资源来源自网络,如果对您有帮助,请点击推荐~. 我尝试了这个方法可以用.电脑重启时,选择从U盘启动,就能安装系统. 参考链接: h ...
随机推荐
- zookeeper的集群部署
1.上传安装包到集群服务器 2.解压 3.修改配置文件 进入zookeeper的安装目录的conf目录 cp zoo_sample.cfg zoo.cfg vi zoo.cfg # The numbe ...
- 微信小程序之 Tabbar(底部选项卡)
1.项目目录 2.在app.json里填写:tab个数范围2-5个 app.json { "pages": [ "pages/index/index", &qu ...
- IIS+Asp.Net Mvc必须知道的事(解决启动/重启/自动回收站点后第一次访问慢问题)
问题现象: Asp.net Mvc站点部署在IIS上后,第一个用户第一次访问站点,都会比较慢,确切的说是访问站点的Action页面(即非静态页面,因为静态页面直接由IIS处理返回给用户即完成请求,而A ...
- Speech Recognition Grammar Specification Version 1.0 JavaScript TTS 文本发音
Speech Recognition Grammar Specification Version 1.0 https://www.w3.org/TR/speech-grammar/ W3C Recom ...
- is id() == 从内存的最小化占用角度解释 我是孕育者,我也应该这样设计 变,必然伴随着加法 一个list是否可以执行set()
def f(a, b): print(a is b, b is a, a == b, a.__eq__(b), id(a), id(b)) f(2, 2) f([2], [2]) f('2', '2' ...
- ios UIWebView 播放优酷土豆视频
将以下的代码嵌套在html里.然后webView载入这个网页.或这段html码,即可了,无须要使用像网上说的html5去兼容 watermark/2/text/aHR0cDovL2Jsb2cuY3Nk ...
- 关于数论【polya计数法】
可以预见数论推公式是有多么蛋疼. 让我简明扼要的讲讲吧(多都说不出来,毕竟才做了两道题)其实呢,这个算法应该归入群论,有个有用的东西:置换群,它表示一个集合包括很多的置换.先讲讲置换吧:↓(这是个置换 ...
- jfreechart应用1--环境配置
jfreechart应用1--环境配置 JFreeChart是一组功能强大.灵活易用的Java绘图API,使用它可以生成多种通用性的报表,包括柱状图.饼图.曲线图.甘特图等.它能够用在Swing和We ...
- jqplot配置参考
jqPlot整的来说有三个地方需要配置.格式如: $.jqplot(‘target’, data, options);target:要显示的位置.data:显示的数据.options:其它配置 ...
- saltstack源码-启动2-parsers.py选项初始化1
class MasterOptionParser(OptionParser, ConfigDirMixIn, MergeConfigMixIn, LogLevelMixIn, RunUserMixin ...