Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
486 views
in Technique[技术] by (71.8m points)

android - ProgressDialog not showing while performing a task

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).

Here's my code at the moment:

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();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Do something like:

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...