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. Java:java+内存分配及变量存储位置的区别

    Java内存区分 Java内存分配与管理是Java的核心技术之一,之前我们曾介绍过Java的内存管理与内存泄露以及Java垃圾回收方面的知识,今天我们再次深入Java核心,详细介绍一下Java在内存分 ...

  2. 混沌数学之Henon吸引子

    Henon吸引子是混沌与分形的著名例子. 相关软件:混沌数学及其软件模拟相关代码: // http://wenku.baidu.com/view/d51372a60029bd64783e2cc0.ht ...

  3. otl翻译(11) -- OTL的迭代器

    OTL stream read iterator 这个类是一个像传统的JDBC中的getter()操作一样扩展了OTL流的模板类.它现在还不支持UNICODE字符集.它对otl_refcur_stre ...

  4. 附2 hystrix详述(2)- 配置

    一.hystrix在生产中的建议 1.保持timeout的默认值(1000ms),除非需要修改(其实通常会修改) 2.保持threadpool的的线程数为10个,除非需要更多 3.依赖标准的报警和监控 ...

  5. Linq-System.Data.Linq.DataContext不包含采用“0”个参数的构造函数

    解决方法: 打开linq to sql 的db文件***.designer.cs,加上下面的代码: 加上这些构造函数之后重新生成就可以了.

  6. Oracle根据字段值找到表名和列名

    方法1: --Oracle 根据字段值查询其所在的表.字段 DECLARE CURSOR cur_query IS SELECT table_name, column_name, data_type ...

  7. MySQL 动态sql语句运行 用时间做表名

    1. 描写叙述 在使用数据的时候,我时候我们须要非常多数据库,并且想用时间来做表名以区分.可是MySQL在存储过程中不支持使用变量名来做表名或者列名. 比方,有一个表我们想以"2015-07 ...

  8. 极客Web前端开发资源集锦

    本周我们带来的前端推荐包含当前热门的bootstrap,html5,css3等技术内容和新闻话题,如果你还想近一步学习如何开发,还可以关注我们的极客课程库,里面涵盖了现代开发技术的‘学’与‘习’的全新 ...

  9. Passing address of non-local object to __autoreleasing parameter for write-back

    在希望通过函数的參数返回Objective-C对象的时候.遇到了这个问题 错误代码例如以下: - (void)methodA:(NSString **)string<span style=&qu ...

  10. Android 之类库常用包

    Android 是由谷歌公司推出的一款基于Linux平台的开源手机操作系统平台. 在Android类库中,各种包写成android.*的方式,重要包的描述如下所示: [1]android.app:提供 ...