android 删除文件进度,android – 从SD卡中删除文件夹时的“旋转轮”进度对话框...
我想用旋转轮显示简单的进度对话框,同时从SD卡中删除文件夹.我有一段代码:ProgressDialog dialog = ProgressDialog.show(this, "","Please wait for few seconds...", true);private void deleteCache() {File f = new File(Environment.getExternalS
我想用旋转轮显示简单的进度对话框,同时从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)之前显示对话框;结束后将其解散.但是我没有看到任何对话框,但是文件夹被删除了.
解决方法:
像这样修改你的代码,
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();
}
}
};
我不确定为什么会这样.由于您的代码在单个线程中执行所有操作,因此progressdialog不会快速显示.因此,尝试在单独的线程中处理其他事情来处理此问题.
标签:android,progressdialog
来源: https://codeday.me/bug/20191002/1843968.html
更多推荐
所有评论(0)