, Android中实现了默认的进度提示对话框,即ProgressDialog,通过实例化和一些简单设置,就可以使用了。

private class DownloadDBTask extends AsyncTask {

// 可变长的输入参数,与AsyncTask.exucute()对应

ProgressDialog pdialog;

public DownloadDBTask(Context context){

pdialog = new ProgressDialog(context, 0);

pdialog.setButton("取消", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int i) {

dialog.cancel();

}

});

pdialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

public void onCancel(DialogInterface dialog) {

finish();

}

});

pdialog.setTitle("第一次使用,正在下载数据...");

pdialog.setCancelable(true);

pdialog.setMax(100);

pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

pdialog.show();

}

@Override

protected String doInBackground(String... params) {

try{

if (DataOper.GetTopNearestPOIs(1, mDBHelper).size()==0)

DataOper.GetAllPtsFromNet(mDBHelper, pdialog); // 从网络上下载数据记录的功能

} catch(Exception e) {

e.printStackTrace();

}

return null;

}

@Override

protected void onCancelled() {

super.onCancelled();

}

@Override

protected void onPostExecute(String result) {

pdialog.dismiss();

}

@Override

protected void onPreExecute() {

}

@Override

protected void onProgressUpdate(Integer... values) {

}

}

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

DownloadDBTask task = new DownloadDBTask(context);

task.execute("");

注意AsyncTask为泛型类,具有三个泛型参数,此处设计为 ,对应于运行参数、进度值类型和返回参数。

从sdk的文档中看到,当一个AsyncTask运行的过程中,经历了4个步骤:

1、onPreExecute(), 在excute调用后立即在ui线程中执行。 This step is normally used to setup the task, for instance by showing a progress bar in the user interface.

2、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.

3、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.

4、onPostExecute, 后台运算完成时在ui线程中调用. The result of the background computation is passed to this step as a parameter.

android 后台耗时,android教程之使用asynctask在后台运行耗时任务相关推荐

  1. Android源码之路(二、AsyncTask)

    参考 https://www.baidu.com/link?url=QNRznJEBT25k0bpgVD3bOniOia2W85eiPIWrS93YFknyrHoFDGrJVtoax2ZYpiiErt ...

  2. 《Android 移动应用基础教程(Android Studio)(第2版)》【课本客观题】+【学习通2023春】【参考答案】

    文章目录 超星学习通智能终端软件开发(基于Android Studio环境)章节作业(39) 一 二 三 四 五 六 课本一 课本二 课本三 课本四 课本五 课本六(无) 课本七 课本八 课本九 课本 ...

  3. 【转】如何选好Android开发书籍和教程[总结]

    Android是Google于2007年11月5日宣布的基于Linux平台的开源手机操作系统的名称,该平台由操作系统.中间件.用户界面和应用软件组成,号称是首个为移动终端打造的真正开放和完整的移动软件 ...

  4. android 发送前台广播,使用IntentService与BroadcastReceiver实现后台服务(Android7.0可用)...

    IntentService的优点 IntentService会创建单独的线程处理所有的Intent请求, 会处理onHandleIntent方法实现的代码, 隐藏开发者无须处理多线程问题, 当所有请求 ...

  5. 使用Android Studio搭建Android集成开发环境(图文教程)

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  6. Android笔记(三十六) AsyncTask是如何执行的?

    在上一个例子中,我们是在LoadImage的onPostExecute中修改的UI,不是说只允许在主线程中修改UI吗?我们看一下源代码是如何操作的. MainActicity.java package ...

  7. [免费专栏] Android安全之Android Xposed插件开发,小白都能看得懂的教程

    也许每个人出生的时候都以为这世界都是为他一个人而存在的,当他发现自己错的时候,他便开始长大 少走了弯路,也就错过了风景,无论如何,感谢经历 Android安全付费专栏长期更新,本篇最新内容请前往: [ ...

  8. Android APP 快速开发教程(安卓)

    Android APP 快速开发教程(安卓) 前言 本篇博客从开发的角度来介绍如何开发一个Android App,需要说明一点是,这里只是提供一个如何开发一个app的思路,并不会介绍很多技术上的细节, ...

  9. Android Studio TV开发教程(十五) Android N及更早版本中的建议

    Android Studio TV开发教程 (转自Android官网https://developer.android.com/training/tv/start) 文章源自:光谷佳武 https:/ ...

最新文章

  1. NAS存储对称和非对称结构之前的区别概述
  2. 2020-12-16(虚析构函数,神逻辑代码)
  3. [BZOJ 2734] 集合选数
  4. C语言链表的来源分析
  5. Jon Wexler and adidas NMD R1 Primeknit Zebra Pack
  6. 李宁-2015年7月13日-个人文档
  7. pd17虚拟机启动器怎么生成?快来看看吧
  8. angular.js 验证码注册登录
  9. asp.net 事件调用事件问题?
  10. 【离散数学·图论】关于哈密顿图的判别条件总结
  11. Android微信emoji表情,Android 软键盘和emoji表情切换方案,和微信几乎一样的体验...
  12. 【Linux】Linux的字符终端
  13. 对打字练的小键人的网站91xjr.com站资源打包,gulp独立分文件夹打包
  14. cocos2dx交叉编译之Android.mk修改
  15. 简单的例子理解正则表达式中的前瞻与后顾
  16. Android adb查看网络连接情况
  17. jQuery带有动画的返回顶部
  18. 当前主流服务器型号,当前服务器的主流品牌 型号 配置
  19. ADM打不开/data,或打开后无法导出里面的文件
  20. 8.2 “三十岁挨踢”的魔咒——《逆袭大学》连载

热门文章

  1. expect java_expect命令
  2. OJ1039: n个数求和(C语言for循环)
  3. 计算机工程与科学北京,计算机工程与科学
  4. linux vim复制粘贴删除,Linux vim删除、复制、粘贴快捷键
  5. 信息学奥赛一本通(1226:装箱问题)
  6. 信息学奥赛一本通(1162:字符串逆序)
  7. 信息学奥数一本通(1004:字符三角形)
  8. 信息学奥赛一本通C++语言——1045:收集瓶盖赢大奖
  9. 功率曲线k值_特性曲线在参数计算中的应用
  10. 浅谈OpenCL之 应用程序总体步骤思路