使用while-loop来报告进度和完成情况是否正确?

Is it correct to use while-loop to report progress and completion?

我已将 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 库?

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

这种任务最好用AsyncTask

开始显示 onPreExecute 的进度,提取 doInBackground 中的文件,并关闭 onPostExecute 中的进度。

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

这还将确保提取工作在后台线程中进行,并保持 UI 线程空闲。