AsyncTask与ProgressDialog使用笔记(安卓在背景运行耗时任务)

AsyncTask用在需要在ui线程中调用、在背景线程中执行耗时任务、并且在ui线程中返回结果的场合。
下面就是一个在背景中运行的AsyncTask的实现DownloadDBTask, Android中实现了默认的进度提示对话框,即ProgressDialog,通过实例化和一些简单设置,就可以使用了。

  1. private class DownloadDBTask extends AsyncTask<String, Integer, String> {   
  2.         // 可变长的输入参数,与AsyncTask.exucute()对应   
  3.         ProgressDialog pdialog;   
  4.         public DownloadDBTask(Context context){   
  5.             pdialog = new ProgressDialog(context, 0);      
  6.             pdialog.setButton("取消", new DialogInterface.OnClickListener() {   
  7.              public void onClick(DialogInterface dialog, int i) {   
  8.               dialog.cancel();   
  9.              }   
  10.             });   
  11.             pdialog.setOnCancelListener(new DialogInterface.OnCancelListener() {   
  12.              public void onCancel(DialogInterface dialog) {   
  13.               finish();   
  14.              }   
  15.             });
  16.             pdialog.setTitle("第一次使用,正在下载数据...");
  17.             pdialog.setCancelable(true);   
  18.             pdialog.setMax(100);   
  19.             pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);   
  20.             pdialog.show();   
  21.         }
  22.         
  23.         @Override  
  24.         protected String doInBackground(String... params) {   
  25.             try{
  26.                     if (DataOper.GetTopNearestPOIs(1, mDBHelper).size()==0)
  27.                             DataOper.GetAllPtsFromNet(mDBHelper, pdialog); // 从网络上下载数据记录的功能
  28.             } catch(Exception e) {   
  29.                     e.printStackTrace();
  30.             }   
  31.             return null;
  32.         }
  33.   
  34.         @Override  
  35.         protected void onCancelled() {   
  36.             super.onCancelled();   
  37.         }   
  38.   
  39.         @Override  
  40.         protected void onPostExecute(String result) {   
  41.             pdialog.dismiss();    
  42.         }   
  43.   
  44.         @Override  
  45.         protected void onPreExecute() {
  46.         }   
  47.   
  48.         @Override  
  49.         protected void onProgressUpdate(Integer... values) {    
  50.         }  
  51.      }   
复制代码


对于写好的异步任务类,调用方法为:

  1. DownloadDBTask task = new DownloadDBTask(context);   
  2.         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.

转载于:https://www.cnblogs.com/xiaochao1234/p/3535881.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值