0
我想要显示对话框进度,当我提取的东西。但我的代码做的工作,但它并没有更新的进展:(这是我的代码Asynctask没有更新进度
主代码
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
我需要做什么? – user3098412
您需要发布进度更新,而不是在完成之后。我将更改解压缩函数,以便您可以在while循环内调用publishProgress。 – ksasq