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. Guava Files 源码分析(一)

    Files中的工厂 Files类中对InputStream, OutputStream以及Reader,Writer的操作封装了抽象工厂模式,抽象工厂是InputSupplier与OutputSupp ...

  2. 四边形优化dp

    理解: http://blog.renren.com/share/263498909/1064362501 http://www.cnblogs.com/ronaflx/archive/2011/03 ...

  3. struts2 18拦截器详解(五)

    I18nInterceptor 该拦截器处理defaultStack第四的位置,是用来方便国际化的,如果说我们的一个Web项目要支持国际化的话,通常的做法是给定一个下拉框列出所支持的语言,当用户选择了 ...

  4. CREATE DATABASE failed

    由于环境需要, 故修改SQL Server 2012的默认的数据库的数据文件和日志文件的位置. 如下: 创建数据库, 遭遇报错. 错误信息: A file activation error occur ...

  5. Objective-C-Category类别

    Object-C开发的时候有的时候会用到Category类,类似于Java和C#中扩展类,就是如果你觉得如果你觉得常用的方法在String中没有,可以根据业务需求和个人喜好写一个扩展类,然后在其中补充 ...

  6. 九度 题目1421:Abor

    转载声明本文地址 http://blog.csdn.net/yangnanhai93/article/details/40563285 题目链接:http://ac.jobdu.com/problem ...

  7. [NPM] Execute Code from a Remote GitHub Branch with npx

    We will see how you can use npx to pull and execute code from a GitHub repository. If you need even ...

  8. [Sass] Level 3: Mixin -- Ex

    When to use MIXIN? Better way to use MIXIN is when you deal with browser prefiex, for example: @mixi ...

  9. [Backbone] Working with forms

    Our first step is to add a template to the AppointmentForm below. Have the template produce a form w ...

  10. MySQL数据源在Spring中的配置

    干脆把MySQL的数据源配置也一起放这里,以备不时之需. MySQL的驱动包可以从这里 http://pan.baidu.com/s/1d02aZ 下载. 以下粗体部分是需要你根据实际情况调整的. & ...