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

【转】 Pro Android学习笔记(九四):AsyncTask(3):ProgressDialog的更多相关文章

  1. 【转】 Pro Android学习笔记(四十):Fragment(5):适应不同屏幕或排版

    目录(?)[-] 设置横排和竖排的不同排版风格 改写代码 对于fragment,经常涉及不同屏幕尺寸和不同的排版风格.我们在基础小例子上做一下改动,在横排的时候,仍是现实左右两个fragment,在竖 ...

  2. 【转】 Pro Android学习笔记(四二):Fragment(7):切换效果

    目录(?)[-] 利用setTransition 利用setCustomAnimations 通过ObjectAnimator自定义动态效果 程序代码的编写 利用fragment transactio ...

  3. 【转】 Pro Android学习笔记(四八):ActionBar(1):Home图标区

    目录(?)[-] Home Icon 源代码 TextView的滚动 返回主activity或指定activity     ActionBar在Android 3.0 SDK中为平板引入,在4.0中也 ...

  4. 【转】 Pro Android学习笔记(四七):Dialog(4):一些补充和思考

    目录(?)[-] 编程思想封装接口 fragment和activity以其他fragment之间的通信 编程思想:封装接口 在小例子中,fragment会调用activity的onDialogDone ...

  5. 【转】 Pro Android学习笔记(四三):Fragment(8):再谈Transaction和管理器

    目录(?)[-] Transaction的一些操作 再谈FragmentManager 调用其他fragment的方法 唤起activity 唤起fragment和相互通信 一些其它 Transact ...

  6. 【转】Pro Android学习笔记(四):了解Android资源(下)

    处理任意的XML文件 自定义的xml文件放置在res/xml/下,可以通过R.xml.file_name来获取一个XMLResourceParser对象.下面是xml文件的例子: <rootna ...

  7. 【转】 Pro Android学习笔记(二九):用户界面和控制(17):include和merge

    目录(?)[-] xml控件代码重用include xml控件代码重用merge 横屏和竖屏landsacpe portrait xml控件代码重用:include 如果我们定义一个控件,需要在不同的 ...

  8. Pro Android学习笔记 ActionBar(1):Home图标区

     Pro Android学习笔记(四八):ActionBar(1):Home图标区 2013年03月10日 ⁄ 综合 ⁄ 共 3256字 ⁄ 字号 小 中 大 ⁄ 评论关闭 ActionBar在A ...

  9. 【转】 Pro Android学习笔记(五六):配置变化

    目录(?)[-] Activity的destorycreate过程 Fragment的destorycreate过程 onSaveInstanceState saveFragmentInstanceS ...

  10. 【转】 Pro Android学习笔记(十九):用户界面和控制(7):ListView

    目录(?)[-] 点击List的item触发 添加其他控件以及获取item数据 ListView控件以垂直布局方式显示子view.系统的android.app.ListActivity已经实现了一个只 ...

随机推荐

  1. java 与大数据学习较好的网站

    C# C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿!https://www.cnblogs.com/doforfuture/p/6293926.html ...

  2. ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syria, Lattakia, Tishreen University, April, 30, 2018

    ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syr ...

  3. 自行实现Kinect 手势Demo踩的坑

    要将继承KinectGestures.GestureListenerInterface的脚本手动赋值给KinectManager脚本的手势监听列表

  4. C#文件与文件夹操作

    (转自:http://www.csharpwin.com/csharpspace/6649r3960.shtml) C#文件操作:C#追加文件 StreamWriter sw = File.Appen ...

  5. MySQL 5.7.3.0 安装 全程截图

    前言: 下一个班快讲MySQL数据库了,正好把服务器里面的MySQL卸了重装了一下. 截个图,作为笔记.也正好留给需要的朋友们. 目录: 下载软件 运行安装程序 安装程序欢迎界面 许可协议 查找更新 ...

  6. LeetCode OJ:Compare Version Numbers(比较版本字符串)

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  7. request对象和response对象,什么时候用,具体用哪一个,没有感觉

    request对象和response对象,什么时候用,具体用哪一个,没有感觉

  8. L144

    Puny Dwarf Planet, Named 'Goblin,' Found Far Beyond PlutoA scrawny dwarf planet nicknamed "the ...

  9. include和application

    include指令 语法:<%@ include  file=”路径+文件名” %> 把指定的文件包含到当前jsp中. application(应用的全局变量) 实现用户之间的数据共享 常 ...

  10. string manipulation in game development-C # in Unity -

    ◇ string manipulation in game development-C # in Unity - It is about the various string ● defined as ...