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. 企业版Oracle10g的安装-过程

    ylbtech-Oracle:企业版Oracle10g的安装-过程 Oracle10g的安装 在Windows操作系统上安装Oracle10g数据库的步骤如下: 0.1)从Oracle的官方网站上下载 ...

  2. crtmpserver实现防盗流和流推送验证

    Protecting your streams from webpage copy&paste flash code, listing or recording 保护流,防止在页面上被复制&a ...

  3. IOS系统之蓝牙外接设备

    Ios系统对于蓝牙外接设备在iphone4以前都是蓝牙2.0的时候,需要通过苹果的审核,据统计通过率仅有2%左右,现在蓝牙2.0基本上处于淘汰状态,所以在这里就不考虑了. 现在iphone4s以后的设 ...

  4. NTP Server

    Network Time Protocol互联网时间协议 NTP is intended to synchronize all participating computers to within a ...

  5. C++的四种初始化形式以及类型转换

      C++中有如下的方式来初始化一个变量. 但当进行类型转换时,只有两种方式可用,其他两种方式会报错.

  6. 如何监控执行的SQL语句?

    环境: SQL Server 2012. 打开SQL Server Profiler. 在菜单中选择New Trace, 连接上SQL Server. 在弹出的窗口中选择Event selection ...

  7. [Node.js]32. Level 7: Working with Lists -- Redis

    As we saw in the video, redis can do more than just simple key-value pairs. We are going to be using ...

  8. jquery ui autocomplete 模拟百度搜索自动提示

    直接上代码 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=" ...

  9. rapidxml 解析修改内存的值

    1.使用rapidxml解析的时候,也就是 调用xmlDoc.parse<0>(xmlContent),特别注意,rapidxml会修改内存的值,把右尖括号>修改为'\0',因此特别 ...

  10. USACO humble

    用set构造,优先队列和堆也能够 /* ID:kevin_s1 PROG:humble LANG:C++ */ #include <iostream> #include <cstdi ...