AsyncTask用在需要在ui线程中调用、在背景线程中执行耗时任务、并且在ui线程中返回结果的场合。
下面就是一个在背景中运行的AsyncTask的实现DownloadDBTask, Android中实现了默认的进度提示对话框,即ProgressDialog,通过实例化和一些简单设置,就可以使用了。

  1. private class DownloadDBTask extends AsyncTask<String, Integer, String> {
  2. // 可变长的输入参数,与AsyncTask.exucute()对应
  3. ProgressDialog pdialog;
  4. public DownloadDBTask(Context context){
  5. pdialog = new ProgressDialog(context, 0);
  6. pdialog.setButton("取消", new DialogInterface.OnClickListener() {
  7. public void onClick(DialogInterface dialog, int i) {
  8. dialog.cancel();
  9. }
  10. });
  11. pdialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
  12. public void onCancel(DialogInterface dialog) {
  13. finish();
  14. }
  15. });
  16. pdialog.setTitle("第一次使用,正在下载数据...");
  17. pdialog.setCancelable(true);
  18. pdialog.setMax(100);
  19. pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  20. pdialog.show();
  21. }
  22. @Override
  23. protected String doInBackground(String... params) {
  24. try{
  25. if (DataOper.GetTopNearestPOIs(1, mDBHelper).size()==0)
  26. DataOper.GetAllPtsFromNet(mDBHelper, pdialog); // 从网络上下载数据记录的功能
  27. } catch(Exception e) {
  28. e.printStackTrace();
  29. }
  30. return null;
  31. }
  32. @Override
  33. protected void onCancelled() {
  34. super.onCancelled();
  35. }
  36. @Override
  37. protected void onPostExecute(String result) {
  38. pdialog.dismiss();
  39. }
  40. @Override
  41. protected void onPreExecute() {
  42. }
  43. @Override
  44. protected void onProgressUpdate(Integer... values) {
  45. }
  46. }

复制代码

对于写好的异步任务类,调用方法为:

  1. DownloadDBTask task = new DownloadDBTask(context);
  2. task.execute("");

复制代码

注意AsyncTask为泛型类,具有三个泛型参数,此处设计为 <String, Integer, String>,对应于运行参数、进度值类型和返回参数。
从sdk的文档中看到,当一个AsyncTask运行的过程中,经历了4个步骤:
  • onPreExecute(), 在excute调用后立即在ui线程中执行。 This step is normally used to setup the task, for instance by showing a progress bar in the user interface.
  • doInBackground, 当 onPreExecute() 完成后, 立即在后台线程中运行. 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 to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate step.
  • onProgressUpdate, 在调用publishProgress后,在ui线程中运行. 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, 后台运算完成时在ui线程中调用. The result of the background computation is passed to this step as a parameter.

AsyncTask与ProgressDialog使用笔记(安卓在背景运行耗时任务)的更多相关文章

  1. 安卓手机上运行 PC-E500 程序

    目录 第1章安卓手机上运行 PC-E500 程序    1 1 PockEmul    1 2 下载    1 3 打包BASIC程序    2 4 配置PC-E500模拟器    5 5 载入e50 ...

  2. 在安卓(手机)上运行 Ubuntu (Linux)

    在安卓(手机)上运行 Ubuntu (Linux) 由于x86 和 arm 是跨平台的,所使用的编译器自然也不同.如果要在电脑上编译安卓手机上的程序,则需在电脑端建立ARM交叉编译环境,这个过程是在耗 ...

  3. 安卓android eclipse运行提示no compatible targets were found

    在eclipse中开发安卓应用,运行项目时,右击项目名称---Run As---Android Application时, 系统提示"No compatible targets were f ...

  4. 笔记-爬虫部署及运行工具-scrapydweb

    笔记-爬虫部署及运行工具-scrapydweb 1.      简介 scrapyd是爬虫部署工具,但它的ui比较简单,使用不是很方便. scrapydweb以scrapyd为基础,增加了ui界面和监 ...

  5. Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案

    的最佳方案 标签: Android屏幕旋转AsyncTaskProgressDialog 2014-07-19 09:25 39227人阅读 评论(46) 收藏 举报 分类: [android 进阶之 ...

  6. Android学习笔记①——安卓工具的基本安装

    安卓已经出来很长时间了,网上的教程也有很多,怕以后忘记,就把网上大牛们的分享的知识自己在学习一下,也记录一下,如果能帮到别人,那是更好不过的! 鉴于现在的IDE工具来说,IDEA已经占据了java的半 ...

  7. Adaptive AUTOSAR 学习笔记 3 - AP 背景、技术及特征(中文翻译)

    本系列学习笔记基于 AUTOSAR Adaptive Platform 官方文档 R20-11 版本.本文从AUTOSAR_EXP_PlatformDesign.pdf开始,一边学习,一边顺带着翻译一 ...

  8. css通用小笔记01——导航背景

    很多刚接触前端的可能遇到一些css能解决的小问题,我现在总结了一些,将会逐渐和大家分享,先是导航的背景问题,在网页中常常看到,当鼠标放到一个导航按钮上面是,就会出现一些特效,比如背景,这是最常用的,我 ...

  9. 【代码笔记】iOS-给背景图加上移动的手势

    一,工程图. 二,效果图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

随机推荐

  1. go语言基础之数组比较和赋值

    1.go语音基础之数组比较和赋值 示例: package main //必须有个main包 import "fmt" func main() { //支持比较,只支持 == 或 ! ...

  2. VMware vCenter中, 如何辩认虚机上Raw Device Mapping过了的一块物理磁盘?

    比如说, 我们有一套VMware的环境, 其中有一台运行者ESXi的主机, 其上有十块SAS盘. 这十块盘中的五块盘被RDM到一台虚机上了. 假设你发现有添加多了一块盘, 你想移除掉, 但是5块盘其中 ...

  3. 【Gson】简介 文档 基本使用 示例

    简介 new TypeToken<List<Person>>() {}.getType() 1 1   1 new TypeToken<List<Person> ...

  4. js判断是否为手机访问

    JavaScript判断浏览器类型一般有两种办法,一种是根据各种浏览器独有的属性来分辨,另一种是通过分析浏览器的userAgent属性来判断的.在许多情况下,值判断出浏览器类型之后,还需判断浏览器版本 ...

  5. CSS文字换行详细解说

    本文列举了兼容 IE 和 FF 地换行 CSS 推荐样式,详细介绍了word-wrap同word-break地区别.兼容 IE 和 FF 地换行 CSS 推荐样式: 最好地方式是 word-wrap: ...

  6. Android之WifiManager

    移动设备离不开网络,android平台中在包android.net.wifi下提供了一些类专门用于管理设备的Wifi功能.该包下主要存在如下几个类: 1.  ScanResult:主要用来描述通过Wi ...

  7. IOS之UITabBarController

    在学习IOS开发过程中,针对于UITabBarController的使用也不少出现,UITabBarController和UINavigationController类似,UITabBarContro ...

  8. spring boot xml与dao 映射关系

    mybatis的xml路径要和 dao的路径一模一样 dao 用@Mapper 注解

  9. LINUX设备驱动程序笔记(五)中断处理

         <一> 中断处理流程例如以下: 1.发生中断时,CPU运行异常向量vector_irq的代码. 2.在vector_irq里面.终于会调用中断处理的总入口函数asm_do_IRQ ...

  10. iOS8开发~Swift(二)Playground

    一.Playground介绍 Playground是Xcode6中自带的Swift代码开发环境.俗话说"功欲善其事,必先利其器".曾经在Xcode5中编写脚本代码.比如编写JS.其 ...