"Rotating wheel" progress dialog while deleting folder from SD card
Asked Answered
R

3

2

I want to display simple progress dialog with rotating wheel, while deleting folder from SD card. I have a following piece of code:

  ProgressDialog dialog = ProgressDialog.show(this, "",
            "Please wait for few seconds...", true);

private void deleteCache() {

    File f = new File(Environment.getExternalStorageDirectory()
            .getAbsoluteFile() + Constants.DATA_DIR);
    deleteDirectory(f);
    dialog.dismiss();
}

 private void deleteDirectory(File path) {
        if (path.exists()) {
            File[] files = path.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteDirectory(files[i]);
                } else {
                    files[i].delete();
                }
            }
        }
        return (path.delete());
    }

Which is supposed to show dialog before deleteDirectory(f); and dissmis it after it ends. But I never see any dialog, event though the folder is being deleted.

Receptacle answered 1/8, 2011 at 6:8 Comment(2)
#6223889Jerid
Did this help? Feel free to accept an answer if it did in order to help others see what helped solve the issue.Antimatter
D
1

Modify your code like this,

 ProgressDialog dialog = ProgressDialog.show(this, "",
        "Please wait for few seconds...", true);
        new Thread(new Runnable() {
        public void run() {
              File f = new File(Environment.getExternalStorageDirectory()
        .getAbsoluteFile() + Constants.DATA_DIR);
deleteDirectory(f);
   private void deleteDirectory(File path) {
    if (path.exists()) {
        File[] files = path.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                deleteDirectory(files[i]);
            } else {
                files[i].delete();
            }
        }
    }
    return (path.delete());
}


            handler.sendEmptyMessage(0);

        }
    }).start();

    handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            try {



                progressDialog.dismiss();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
        }
    };

I am not sure why this happens. Since your code does everything in a single thread, progressdialog will not show up quickly. So instead trying to handle other things in a separate thread handles this problem.

Dorcy answered 1/8, 2011 at 6:16 Comment(3)
It is correct but AsyncTask is a better way to do it as you get the three stages as functions and can pass info from one to the other.Antimatter
I cannot understand this answer because what is handler used here is the instant of which class ?Winnick
handler is not any instance of class. Kindly go through this developer.android.com/reference/android/os/Handler.htmlDorcy
A
4

This answer is also all over StakcOverflow. Use AsyncTask which will run on a different thread and has three stages... One pre which you will load the wheel in and the post which you will dismiss it when done... And then the background which is the actual work.

Antimatter answered 1/8, 2011 at 6:20 Comment(1)
Not sure the edit was necessary but oh well. The important thing here is that ASyncTask is the intended function for this. It has three functions, pre, during, and post which allow for a lot of power. Take a look at the link. You will learn a lot.Antimatter
E
2
private void deleteCache() {
  ProgressDialog dialog = ProgressDialog.show(this, "",
    "Please wait for few seconds...", true);

  Runnable myRun = new Runnable() {
    public void run() {
      File f = new File(Environment.getExternalStorageDirectory()
        .getAbsoluteFile() + Constants.DATA_DIR);
      deleteDirectory(f);

      runOnUiThread(new Runnable() {
        @Override
        public void run() {
          dialog.dismiss();
        }
      });
    }
  }
}

private void deleteDirectory(File path) {
  if (path.exists()) {
    File[] files = path.listFiles();
    for (int i = 0; i < files.length; i++) {
      if (files[i].isDirectory()) {
        deleteDirectory(files[i]);
      } else {
        files[i].delete();
      }
    }
  }

  return (path.delete());
}
Entangle answered 1/8, 2011 at 6:20 Comment(2)
Could you add at least some explanation?Headphone
No I do not want to add any explanation when it is such a detailed question with much code included and a completely worthless titleEntangle
D
1

Modify your code like this,

 ProgressDialog dialog = ProgressDialog.show(this, "",
        "Please wait for few seconds...", true);
        new Thread(new Runnable() {
        public void run() {
              File f = new File(Environment.getExternalStorageDirectory()
        .getAbsoluteFile() + Constants.DATA_DIR);
deleteDirectory(f);
   private void deleteDirectory(File path) {
    if (path.exists()) {
        File[] files = path.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                deleteDirectory(files[i]);
            } else {
                files[i].delete();
            }
        }
    }
    return (path.delete());
}


            handler.sendEmptyMessage(0);

        }
    }).start();

    handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            try {



                progressDialog.dismiss();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
        }
    };

I am not sure why this happens. Since your code does everything in a single thread, progressdialog will not show up quickly. So instead trying to handle other things in a separate thread handles this problem.

Dorcy answered 1/8, 2011 at 6:16 Comment(3)
It is correct but AsyncTask is a better way to do it as you get the three stages as functions and can pass info from one to the other.Antimatter
I cannot understand this answer because what is handler used here is the instant of which class ?Winnick
handler is not any instance of class. Kindly go through this developer.android.com/reference/android/os/Handler.htmlDorcy

© 2022 - 2024 — McMap. All rights reserved.