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

编程入门 行业动态 更新时间:2024-10-04 07:31:01

【转】 Pro Android<a href=https://www.elefans.com/category/jswz/34/1770117.html style=学习笔记(九四):AsyncTask(3):ProgressDialog"/>

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

  

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

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

转载于:.html

更多推荐

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

本文发布于:2024-02-28 13:43:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1769984.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:学习笔记   Android   Pro   AsyncTask   ProgressDialog

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!