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

  

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

Progress Dialog小例子ide

咱们经过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) {
        … … 
    } 
}this

在主线程中AsyncTask的代码:.net

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

上面的进度框不能精确显示进展状况,称为indeterministic进度框。更多的时候咱们但愿能显示进展程度,这就是deterministic进度框,如图所示:blog

相关代码以下:接口

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开发相关文章

相关文章
相关标签/搜索