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 ...
随机推荐
- 数据库实例: STOREBOOK > 表空间 > 编辑 表空间: TEMP
ylbtech-Oracle:数据库实例: STOREBOOK > 表空间 > 编辑 表空间: TEMP 表空间 > 编辑 表空间: TEMP 1. 一般信息返回顶部 1 ...
- 【c语言】使用gumbo解析HTML
之前使用过PHP的Simple HTML DOM简单地解析HTML但PHP终非我所熟悉的语言,虽然我并不对语言抱有绝对的执着= =(什么你不相信,好吧,不管你信不信,反正我是信了= =).虽然可以简单 ...
- 记录cocos2d-html5与cocosd-x jsb中遇到的坑
这两天开始用coco2d-html5写游戏, 但最终是发布到手机上, 写的js代码是跑在jsb上的. 在此记录下遇到的坑. 注:cocos2d-x 简称 cx, cocos2d-html5 简称ch ...
- 倒计时实现方案总结 Timer Handler
利用Timer实现倒计时 @BindView(R.id.send) Button send;//发送验证码 private int time = 60;//倒计时 private Timer time ...
- 使用ECharts实现数据图表分析
一.ECharts介绍 实现对统计数据的图形分析之前用过JFreeChar,但它是用纯java实现编码繁琐且效果不佳,后来又使用过Fusioncharts 报表工具,它是基于Flash的图表组件.以X ...
- Sql Server的弱口令入侵测试以及防范
我之所以要做这样的测试是因为这一切都是有背景的!!!被黑客黑的代价!!!伤心ing..... 背景: 测试: 物理机:Win7 虚拟机:xp 扫描工具(我想我最好不写扫描器的名字比较好): 密码之所以 ...
- 极客Web前端开发资源集锦
本周我们带来的前端推荐包含当前热门的bootstrap,html5,css3等技术内容和新闻话题,如果你还想近一步学习如何开发,还可以关注我们的极客课程库,里面涵盖了现代开发技术的‘学’与‘习’的全新 ...
- ZH奶酪:AngularJS判断checkbox/复选框是否选中并实时显示
最近做了一个选择标签的功能,把一些标签展示给用户,用户选择自己喜欢的标签,就类似我们在购物网站看到的那种过滤标签似的: 简单的效果如图所示: 首先看一下html代码: <!DOCTYPE htm ...
- python中的lambda知多少!
python允许使用lambda关键字创造匿名函数,匿名函数是因为不需要以标准的方式来声明,比如说,使用def语句.(除非赋值给一个局部变量,这样的对象也不会再任何的名字空间内创建名字)然而,作为函数 ...
- LESS详解之编译LESS
掌握LESS,必须先掌握LESS的编译.因为LESS是CSS预处理语言的一种,是一种动态语言.LESS可以运行在各种语言和环境中,包括浏览器端.服务器端等.就因为是一种CSS预处理语言,所以需要编 ...