android教程之使用asynctask在后台运行耗时任务

 更新时间:2014年02月14日 12:53: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个步骤:

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 搜索框架使用详解

    这篇文章主要为大家介绍了Android 搜索框架使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • android使用handlerthread创建线程示例

    android使用handlerthread创建线程示例

    这篇文章主要介绍了android使用handlerthread创建线程,讲解了这种方式的好处及为什么不使用Thread类的原因
    2014-01-01
  • Android Framework Application Framework层简单介绍

    Android Framework Application Framework层简单介绍

    这篇文章主要介绍了 Android Framework Application Framework层简单介绍的相关资料,需要的朋友可以参考下
    2016-11-11
  • Android 欢迎全屏图片详解及实例代码

    Android 欢迎全屏图片详解及实例代码

    这篇文章主要介绍了Android 欢迎全屏图片详解及实例代码的相关资料,需要的朋友可以参考下
    2017-02-02
  • Android 通过API获取数据库中的图片文件方式

    Android 通过API获取数据库中的图片文件方式

    这篇文章主要介绍了Android 通过API获取数据库中的图片文件方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • Kotlin基础学习之循环和异常

    Kotlin基础学习之循环和异常

    最近在学习kotlin,Kotlin 是一个基于 JVM 的新的编程语言,下面这篇文章主要给大家介绍了关于Kotlin基础学习之循环和异常的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-12-12
  • android实现长图加载效果

    android实现长图加载效果

    这篇文章主要为大家详细介绍了android实现长图加载效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-08-08
  • 解析Kotlin JSON格式

    解析Kotlin JSON格式

    这篇文章主要介绍了Kotlin JSON格式解析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03
  • Android进阶手写IPC通信框架告别繁琐AIDL

    Android进阶手写IPC通信框架告别繁琐AIDL

    这篇文章主要为大家介绍了Android进阶手写IPC通信框架告别繁琐AIDL实现详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • Android二级缓存加载图片实现照片墙功能

    Android二级缓存加载图片实现照片墙功能

    这篇文章主要为大家详细介绍了Android二级缓存加载图片实现照片墙功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07

最新评论