我希望在提取内容时显示对话框进度。但我的代码完成了工作,但它没有更新进度:(。这是我的代码
主要代码
class Install_Web extends AsyncTask<String, Integer, Void> {
/**
* Before starting background thread Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
@Override
protected Void doInBackground(String... params) {
try {
copyAssets();
String zipFile = server_Runner.getHttpDirectory() + "/www/www (2).zip";
String unzipLocation = server_Runner.getHttpDirectory() + "/";
Decompress d = new Decompress(zipFile, unzipLocation);
d.unzip();
publishProgress((int)((ze.getName().length()*100)/ze.getSize()));
}
catch (Exception e) {
}
return null;
}
@Override
protected void onProgressUpdate(Integer... m) {
pDialog.setProgress((m[0]));
}
/**
* After completing background task
* Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(Void res) {
dismissDialog(progress_bar_type);
}
}
进度对话
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type :
// we set this to 0
pDialog = new ProgressDialog(this);
pDialog.setMessage("Installing file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}
提取器代码:
public class Decompress {
private String _zipFile;
private String _location;
public Decompress(String zipFile, String location) {
_zipFile = zipFile;
_location = location;
_dirChecker("");
}
public void unzip() {
try {
FileInputStream fin = new FileInputStream(_zipFile);
ZipInputStream zin = new ZipInputStream(fin);
while ((ze = zin.getNextEntry()) != null) {
Log.v("Decompress", "Unzipping " + ze.getName());
if(ze.isDirectory()) {
_dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(_location + ze.getName());
for ( c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
} catch(Exception e) {
Log.e("Decompress", "unzip", e);
}
}
private void _dirChecker(String dir) {
File f = new File(_location + dir);
if(!f.isDirectory()) {
f.mkdirs();
}
}
}
我没有发布与进度无关的代码.. okkk
答案 0 :(得分:0)
解压缩操作是同步的,所以即使你在代码进入publishProgress调用时在后台线程上运行,你已经完成了操作,所以它只会发布100%。
我认为您需要重构您的代码,以便在您的unzip()函数中,您可以在while循环的每次迭代中调用publishProgress,或类似的东西。