AsyncTask与ProgressDialog使用笔记(安卓在背景运行耗时任务)
AsyncTask用在需要在ui线程中调用、在背景线程中执行耗时任务、并且在ui线程中返回结果的场合。
下面就是一个在背景中运行的AsyncTask的实现DownloadDBTask, Android中实现了默认的进度提示对话框,即ProgressDialog,通过实例化和一些简单设置,就可以使用了。
- private class DownloadDBTask extends AsyncTask<String, Integer, String> {
- // 可变长的输入参数,与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("");
复制代码
- 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使用笔记(安卓在背景运行耗时任务)的更多相关文章
- 安卓手机上运行 PC-E500 程序
目录 第1章安卓手机上运行 PC-E500 程序 1 1 PockEmul 1 2 下载 1 3 打包BASIC程序 2 4 配置PC-E500模拟器 5 5 载入e50 ...
- 在安卓(手机)上运行 Ubuntu (Linux)
在安卓(手机)上运行 Ubuntu (Linux) 由于x86 和 arm 是跨平台的,所使用的编译器自然也不同.如果要在电脑上编译安卓手机上的程序,则需在电脑端建立ARM交叉编译环境,这个过程是在耗 ...
- 安卓android eclipse运行提示no compatible targets were found
在eclipse中开发安卓应用,运行项目时,右击项目名称---Run As---Android Application时, 系统提示"No compatible targets were f ...
- 笔记-爬虫部署及运行工具-scrapydweb
笔记-爬虫部署及运行工具-scrapydweb 1. 简介 scrapyd是爬虫部署工具,但它的ui比较简单,使用不是很方便. scrapydweb以scrapyd为基础,增加了ui界面和监 ...
- Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案
的最佳方案 标签: Android屏幕旋转AsyncTaskProgressDialog 2014-07-19 09:25 39227人阅读 评论(46) 收藏 举报 分类: [android 进阶之 ...
- Android学习笔记①——安卓工具的基本安装
安卓已经出来很长时间了,网上的教程也有很多,怕以后忘记,就把网上大牛们的分享的知识自己在学习一下,也记录一下,如果能帮到别人,那是更好不过的! 鉴于现在的IDE工具来说,IDEA已经占据了java的半 ...
- Adaptive AUTOSAR 学习笔记 3 - AP 背景、技术及特征(中文翻译)
本系列学习笔记基于 AUTOSAR Adaptive Platform 官方文档 R20-11 版本.本文从AUTOSAR_EXP_PlatformDesign.pdf开始,一边学习,一边顺带着翻译一 ...
- css通用小笔记01——导航背景
很多刚接触前端的可能遇到一些css能解决的小问题,我现在总结了一些,将会逐渐和大家分享,先是导航的背景问题,在网页中常常看到,当鼠标放到一个导航按钮上面是,就会出现一些特效,比如背景,这是最常用的,我 ...
- 【代码笔记】iOS-给背景图加上移动的手势
一,工程图. 二,效果图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...
随机推荐
- Java 连接MS Access数据库
java连接MS Access的两种方式: 1.JDBC-ODBC Java连接Access可以使用MS自带的管理工具-->数据源(ODBC)设置建立连接,这样就不需要导入jar.但是,如此一来 ...
- Linux C Socket编程发送结构体、文件详解及实例
利用Socket发送文件.结构体.数字等,是在Socket编程中经常需要用到的.由于Socket只能发送字符串,所以可以使用发送字符串的方式发送文件.结构体.数字等等. 本文:http://www.c ...
- IOS Xib使用——Xib表示局部界面
Xib文件是一个轻量级的用来描述局部界面的文件,在之前的文章讲解了为控制器添加Xib文件,本节主要讲解一下通过xib文件表示局部界面. <一> 创建Xib文件 Xib文件创建的时候是选择U ...
- 黑马day12 DbUtils的介绍
简单介绍: DbUtils为不喜欢hibernate框架的钟爱.它是线程安全的,不存在并发问题. 使用步骤: 1. QueryRunner runner=new QueryRunner(这里写数据源. ...
- GridControl 获取某分组的第一个孩子
int iGroupRowHandle = this.gridControlView.FocusedRowHandle; ) { int iChildCount = this.gridControl. ...
- 思维导图软件xmind和mindmanager哪个更好
思维导图是一种将放射性思考具体化的方法,可以将人们的创造性思维及时捕捉并呈现,目前便捷的网络为人们带来了众多的思维导图软件,而在这些软件中只有亲身实践体验过,才能知道到底思维导图哪个好,哪个又适合自己 ...
- Eclipse 创建文件快捷菜单、避免格式化时自动换行、.properties文件中文乱码、在线安装FreeMarker
创建文件快捷菜单设置 打开窗口“Customize Perspective - Java EE”,切换选项卡到“Shortcuts”: 进行一下配置: “Generate”:如上图勾选方式 " ...
- GoogleCpp风格指南 8)格式 _part1
8 格式 Formatting 代码风格和格式确实比較任意, 但一个项目中全部人遵循同一风格是非常easy的; 个体未必允许下述每一处格式规则, 但整个项目服从统一的编程风格是非常重要的, 仅仅有这样 ...
- 如何使用Total Recorder录制软件发出的声音
1 打开Total Recorder的选项,点击系统设置,在弹出的声音选项卡中把Total Recorder扬声器设为默认(选中该项再点击默认,如果第一个扬声器选项还保存着"默认通信设备&q ...
- Activiti Designer 5.14.1插件安装和使用
1.离线包下载 离线安装包下载:https://files.cnblogs.com/files/modou/Activiti_BPMN_2.0_designer.rar 2.安装 先把jars文件夹中 ...