Pro Android学习笔记(九四):AsyncTask(3):ProgressDialog

来源:互联网 发布:隐式差分如何编程 编辑:程序博客网 时间:2024/05/21 07:49

文章转载只能用于非商业性质,且不能带有虚拟货币、积分、注册等附加条件。转载须注明出处:http://blog.csdn.net/flowingflying/

Progress Dialog小例子

我们通过IReportBack接口隐藏了Activity,但是有时我们需要弹框等操作,就需要Context。下面的例子是在执行的过程中弹出Progress Dialog来提示正在处理中。

和MyLongTaskTwo相同的处理的代码将不列出。

public class MyLongTaskTwo extends AsyncTask<String,Integer,Integer>{
    private IReportBack report = null;
    private    Context context = null;
    private String tag = null;
    private ProgressDialog pd = null;

    //在AsyncTask中进行弹框处理,需要在构造函数中传递Context。
    public MyLongTaskTwo(IReportBack inr, Context inCont, String inTag){
        report = inr;
        context = inCont;
        tag = inTag;
    }     
    @Override
    protected void onPreExecute() {
        pd = ProgressDialog.show(context, tag, "In progress.... ");  //显示进度框,这里需要context
    } 
    @Override
    protected void onProgressUpdate(Integer... values) {
        …..
    }
 
    @Override
    protected void onPostExecute(Integer result) {
        ……
        pd.cancel(); //取消进度框的显示
    }     
    @Override
    protected Integer doInBackground(String... params) {
        … …
    }

}

在主线程中AsyncTask的代码:

private void testProgressDialog(){ 
    MyLongTaskTwo task = new MyLongTaskTwo(this, this, "TaskTwo");//传递context
    task.execute("TaskTwo","File","Edit","Refactor","Source","Navigate", Help");
}

上面的进度框不能精确显示进展情况,称为indeterministic进度框。更多的时候我们希望能显示进展程度,这就是deterministic进度框,如图所示:

相关代码如下:

public class MyLongTaskThree extends AsyncTask<String,Integer,Integer> 
  implements DialogInterface.OnCancelListener{     
    @Override //ProgressDialog被cancel时触发的回调函数,处理pd.cancel()会触发外,如果我们按了返回键,也会触发onCancel,我们可以在此进行关闭async任务的处理,否则任务的worker线程将继续执行。
    public void onCancel(DialogInterface dialog)
        report.reportBack(tag,"Cancel Called");
    }
    ... ... 
    private ProgressDialog pd = null;
    
    public MyLongTaskThree(IReportBack inr, Context inCont, String inTag,int inLen){
        ... ...
    }
     
    @Override
    protected void onPreExecute() { 
        pd = new ProgressDialog(context);
        pd.setTitle(tag);
        pd.setMessage("In progressing");
        pd.setCancelable(true);
        pd.setOnCancelListener(this);  //设置cancel的回调函数
        pd.setIndeterminate(false);  //表明是个detemininate精确显示的进度框
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
        pd.setMax(length);
        pd.show();

    }
    @Override
    protected void onProgressUpdate(Integer... values) {
        ... ... 
        pd.setProgress(i+1);
    }
    @Override
    protected void onPostExecute(Integer result) {
        ... ... 
        pd.cancel();
    }     
    @Override
    protected Integer doInBackground(String... params) { 
        int num = params.length;
        for(int i = 0; i < num;i ++){
            Utils.sleepForSecs(2);

            publishProgress(i);
        }       
        return num;
    }

}

相关小例子源代码可在Pro Android学习:AsyncTask小例子中下载。

相关链接: 我的Android开发相关文章

1 0