我试图实现Async来处理带有progresbar的文件的后台解压缩,但是当解压缩progresbar解除0%时,进度条显示0%时发生解压缩... onProgressUpdate方法没有更新。我的代码如下:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ib = (ImageButton) findViewById(R.id.ImageButton01);
ib.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
new DownloadFileFromURL().execute(file_url);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
System.out.println("DIALOG:");
switch (id) {
case progress_bar_type: //
pDialog = new ProgressDialog(this);
pDialog.setMessage("Extracting. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}
private void fileZ()
{
// TODO Auto-generated method stub
String zipFile = myDEST + myFolderImages + myFILEposterZIP;
String unzipLocation = myDEST + myFolderImages;
Decompress d = new Decompress(zipFile, unzipLocation);
d.unzip();
System.out.println("UNZIPPING");
}
class DownloadFileFromURL extends AsyncTask<String, String, String> {
@SuppressWarnings("deprecation")
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
fileZ();
return null;
}
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
}
}
}
编辑#1:
class ExtractFiles extends AsyncTask<String, String, String> {
@SuppressWarnings("deprecation")
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
@Override
protected String doInBackground(String... f_url) {
// TODO Auto-generated method stub
Thread thread = new Thread()
{
public void run()
{
int prog = 0;
while(prog < 100)
{
pDialog.setProgress(prog);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
prog ++;
}
}
};
thread.start();
System.out.println("EXTRACTING...");
fileZ();
return null;
}
答案 0 :(得分:0)
解压缩文件时,您无法正确更新进度条。
使用类似的计数器:
count++;
并更新进度条..
publishProgress((int)((count));