zoukankan      html  css  js  c++  java
  • 【转】 Pro Android学习笔记(九四):AsyncTask(3):ProgressDialog

      

    文章转载只能用于非商业性质,且不能带有虚拟货币、积分、注册等附加条件。转载须注明出处: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开发相关文章

  • 相关阅读:
    poj 1113 Wall 凸包的应用
    NYOJ 78 圈水池 (入门级凸包)
    Monotone Chain Convex Hull(单调链凸包)
    poj Sudoku(数独) DFS
    poj 3009 Curling 2.0(dfs)
    poj 3083 Children of the Candy Corn
    Python join()方法
    通过FISH和下一代测序检测肺腺癌ALK基因融合比较
    华大病原微生物检测
    NGS检测ALK融合大起底--转载
  • 原文地址:https://www.cnblogs.com/blongfree/p/5048104.html
Copyright © 2011-2022 走看看