0

我已将zip4j库添加到我的 Android 项目中。这是非常容易使用。

ZipFile zipFile = new ZipFile(downloadedFilePath);
zipFile.extractAll(context.getFilesDir().getPath());

但是有一个我担心的功能。他们没有订阅以获取解压缩的进度,而是使用while-loop如下:

while (pm.getState() == ProgressMonitor.STATE_BUSY) {
    // my progress handler is here
}

如果我需要知道解压缩是否完成,我应该按如下方式使用它:

while (pm.getState() == ProgressMonitor.STATE_BUSY) {
    // the loop runs until progress monitor changes its status
}
if (pm.getResult() == ProgressMonitor.RESULT_SUCCESS) {
    // my code to handle completion
}

对我来说,它看起来是反模式和糟糕的编码。我应该在我的项目中使用,还是切换到不同的 zip 库?

4

2 回答 2

1

while循环将使用 CPU 资源并保持忙碌。我建议使用Thread并休眠一段时间,然后再次检查pm.getState()并更新您的进度条

于 2015-06-02T10:37:08.400 回答
1

最好AsyncTask用于此类任务。

开始显示进度onPreExecute,提取文件doInBackground,然后关闭进度onPostExecute

您还可以使用onProgresUpdate来显示工作的确切进度。

这也将确保提取工作发生在后台线程中,并使 UI 线程保持空闲。

于 2015-06-02T10:45:38.837 回答