我想用旋转轮显示简单的进度对话框,同时从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

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐