“旋转轮”从 SD 卡删除文件夹时的进度对话框
我想在从 SD 卡删除文件夹时用旋转轮显示简单的进度对话框。我有以下一段代码:
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());
}
应该在 deleteDirectory(f);
之前显示对话框,并在结束后将其关闭。但我从未看到任何对话框,尽管该文件夹已被删除。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个答案也遍布 StakcOverflow。使用 AsyncTask 它将在不同的线程上运行并具有三个阶段。一个是您将加载轮子的前一个,一个是完成后您将关闭它的后一个……然后是实际工作的背景。
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.
像这样修改你的代码,
我不确定为什么会发生这种情况。由于您的代码在单个线程中完成所有操作,因此进度对话框不会很快显示。因此,尝试在单独的线程中处理其他事情可以解决这个问题。
Modify your code like this,
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.