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 ...
随机推荐
- vim配置为IDE环境(超详细,极力推荐 git)
https://github.com/yangyangwithgnu/use_vim_as_ide 1. 用法 git clone https://github.com/VundleVim/Vundl ...
- javascript 语法规范错误提示代码
“Missing semicolon.” : “缺少分号.”, “Use the function form of \”use strict\”.” : “使用标准化定义function.”, “Un ...
- PHP swfupload图片上传实例
swfupload已经是第二次研究,这次自已整了个简单demo,无奈菜鸟最杯… PHP代码如下: if (isset($_FILES["Filedata"]) || !is_upl ...
- udhcp源码详解(三)上 之配置信息的读取
上节介绍了存储管理配置信息的结构体struct server_config_t,该结构体贯穿整个server端程序的运行. 在dhcpd.c里的用该结构体定义个一个全局的变量: struct serv ...
- IIS老革命遇到的一些问题
今天部署一个网站到IIS,遇到了一些问题.老革命遇上新问题.前不久搞java,接触了一下tomcat,觉得真麻烦.而tomcat大概是java阵营中最简单的了吧.想不到,IIS7,友好的图形界面下,也 ...
- Linux常用服务安装部署
1,centos7默认是装有python的,检查python版本的命令 # 检查python版本 : python -V 2,centOS在安装python3以及tab补全功能 下载python3源码 ...
- 授权QQ登录的qq端前端页面变迁
ac_type = 'qq' if ac_type == 'qq': myid, mypwd = qq_key xp = '/html/body/div/div/div[2]/div/div/div/ ...
- Java内部类用法
内部类可以是静态(static)的,可以使用 public.protected 和 private 访问控制符,而外部类只能使用 public,或者默认. 成员式内部类 在外部类内部直接定义(不在方法 ...
- XML解析(DOM)
001 public class DOM_Parser { 002 003 public static void main(String[] args) { 004 try ...
- 并不对劲的bzoj5340:loj2552:uoj399:p4564: [Ctsc2018]假面
题目大意 有\(n\)(\(n\leq200\))个非负整数\(m_1,m_2,...,m_n\)(\(\forall i\in[1,n],m_i\leq100\)),有\(q\)(\(q\leq2* ...