执行任务时未显示ProgressDialog [英] ProgressDialog not showing while performing a task

查看:104
本文介绍了执行任务时未显示ProgressDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个备份例程,可以将所有内容从一个文件夹复制到运行良好的外部SD卡中.我试图获得一个不错的弹出对话框,该对话框在运行时显示但只是未显示.甚至不尝试运行(但备份确实完成了).

I have a backup routine that copies everything from one folder to an external SD card which works perfectly. I'm trying to get an nice popup dialog box that shows when it's running but it just isn't showing. Doesn't even attempt to run (but the backup does complete).

这是我的代码:

public void doBackup(View view) throws IOException{
    ProgressDialog pd = new ProgressDialog(this);
    pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    pd.setMessage("Running backup. Do not unplug drive");
    pd.setIndeterminate(true);
    pd.setCancelable(false);
    pd.show();
    File source = new File("/mnt/extSdCard/DirectEnquiries"); 
    File dest = new File("/mnt/UsbDriveA/Backup");
    copyDirectory(source, dest);
    pd.dismiss();
}

推荐答案

您可以在ThreadAsyncTask中运行长时间运行的任务.然后您的ProgressDialog将会显示.

You run long running tasks in a Thread or with an AsyncTask. Then your ProgressDialog will show up.

执行以下操作:

public void doBackup(View view) throws IOException{
    final ProgressDialog pd = new ProgressDialog(this);
    pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    pd.setMessage("Running backup. Do not unplug drive");
    pd.setIndeterminate(true);
    pd.setCancelable(false);
    pd.show();
    Thread mThread = new Thread() {
        @Override
        public void run() {
            File source = new File("/mnt/extSdCard/DirectEnquiries"); 
            File dest = new File("/mnt/UsbDriveA/Backup");
            copyDirectory(source, dest);
            pd.dismiss();
        }
    };
    mThread.start();
}

这篇关于执行任务时未显示ProgressDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆