“Rotating wheel” progress dialog while deleting folder from SD card

淺唱寂寞╮ 提交于 2020-01-10 05:31:25

问题


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.


回答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.




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/6894698/rotating-wheel-progress-dialog-while-deleting-folder-from-sd-card

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!