Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 582|回复: 0

[实例教程]在背景运行耗时任务,AsyncTask与ProgressDialog的使

[复制链接]

该用户从未签到

发表于 2011-10-22 13:31:06 | 显示全部楼层 |阅读模式
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("");

注意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.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2024-5-19 04:50 , Processed in 0.357365 second(s), 34 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表