使用进度条删除文件夹和内容

时间:2016-04-01 15:52:38

标签: java android

我想删除带有进度条的文件夹和内容 ,请帮帮我

public interface java.time.chrono.ChronoLocalDateTime<D extends java.time.chrono.ChronoLocalDate> ex
tends java.time.temporal.Temporal, java.time.temporal.TemporalAdjuster,

3 个答案:

答案 0 :(得分:1)

使用AsyncTask。 E.g。

private class BackgroundTask extends AsyncTask <Void, Void, Void> {
    private ProgressDialog dialog;

    public BackgroundTask(MyMainActivity activity) {
        dialog = new ProgressDialog(activity);
    }

    @Override
    protected void onPreExecute() {
        dialog.setMessage("Doing something, please wait.");
        dialog.show();
    }

    @Override
    protected void onPostExecute(Void result) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    protected Void doInBackground(Void... params) {
        //do your work

        return null;
    }

}

如果您想报告进度,可以使对话不会不确定并实施onProgressUpdate。然后,您可以在publishProgress中使用doInBackground

答案 1 :(得分:0)

为此使用asynctask。

以下是一个示例: - http://android-er.blogspot.in/2015/02/progressdialog-and-asynctask.html?m=1

答案 2 :(得分:0)

您可以使用异步任务删​​除目录中的文件和子文件夹。 最好通过下面的代码。

public class DeleteExtractorTask extends AsyncTask<Void, Integer, Long> {
    private Context mContext;
    private final ProgressDialog mDialog;
    private int total;
    private int curPosi;
    private String path;
 
    public DeleteExtractorTask(String filePath, int total, Context context) {
        if (context != null) {
            mDialog = new ProgressDialog(context);
        } else {
            mDialog = null;
        }
        mContext = context;
        this.total = total;
        this.path = filePath;
    }
 
    @Override
    protected Long doInBackground(Void... voids) {
        publishProgress(0, total);
        deleteDirectory(path);
        return null;
    }
 
    @Override
    protected void onPreExecute() {
        if (mDialog != null) {
            mDialog.setTitle ( "total" + total + "files");
                         mDialog.setMessage ( "removed");
            mDialog.setCanceledOnTouchOutside(false);
            mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mDialog.show();
        }
    }
 
    @Override
    protected void onPostExecute(Long aLong) {
        super.onPostExecute(aLong);
        if (mDialog != null && mDialog.isShowing()) {
            FileUtils.mkdirFiles();
            mDialog.dismiss();
        }
        if (isCancelled())
            return;
 
    }
 
    @Override
    protected void onProgressUpdate(Integer... values) {
        if (mDialog == null)
            return;
        if (values.length > 1) {
            int max = values[1];
            mDialog.setMax(max);
        } else {
            mDialog.setProgress(values[0].intValue());
        }
    }
 
    private boolean deleteDirectory(String dir) {
                 // If dir is not separated from the end of the character to the file, the file is automatically added delimiter
        if (!dir.endsWith(File.separator))
            dir = dir + File.separator;
        File dirFile = new File(dir);
                 // If dir corresponding file does not exist or is not a directory, then exit
        if ((!dirFile.exists()) || (!dirFile.isDirectory())) {
            return false;
        }
        boolean flag = true;
                 // delete all the files in the folder, including subdirectories
        File[] files = dirFile.listFiles();
        for (int i = 0; i < files.length; i++) {
                         // delete subfolders
            if (files[i].isFile()) {
                curPosi++;
                publishProgress(curPosi);
                flag = FileUtils.deleteFile(files[i].getAbsolutePath());
                if (!flag)
                    break;
            }
                         // delete subdirectories
            else if (files[i].isDirectory()) {
                curPosi++;
                publishProgress(curPosi);
                flag = deleteDirectory(files[i]
                        .getAbsolutePath());
                if (!flag)
                    break;
            }
        }
        if (!flag) {
            return false;
        }
                 // delete the current directory
        if (dirFile.delete()) {
            return true;
        } else {
            return false;
        }
    }
}