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

所属分类: 软件编程 / Android 阅读数: 1781
收藏 0 赞 0 分享

, 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手机中很常见的一个实用功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Android实现Activity界面切换添加动画特效的方法

这篇文章主要介绍了Android实现Activity界面切换添加动画特效的方法,非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中Dialog去黑边的方法

这篇文章主要介绍了Android中Dialog去黑边的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Qt for Android开发实例教程

这篇文章主要介绍了Qt for Android开发的方法,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之时间日期操作实例

这篇文章主要介绍了Android开发之时间日期操作,是Android程序开发中常见的一个功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之时间日期组件用法实例

这篇文章主要介绍了Android开发之时间日期组件用法,主要介绍了TimePicker和DatePicker组件,对于Android程序开发有不错的借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之获取网络链接状态

这篇文章主要介绍了Android获取网络链接状态的方法,主要是通过ConnectivityManager类来完成的,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之广播机制浅析

这篇文章主要介绍了Android开发之广播机制浅析,主要包括了发布、接收及配置广播的实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之登录验证实例教程

这篇文章主要介绍了Android开发之登录验证实现方法,包括发送数据、服务器端验证、配置文件等,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之注册登录方法示例

这篇文章主要介绍了Android开发的注册登录方法,是针对Android程序设计中版本兼容性的进一步完善,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多