我有一个由 a 管理的进度条,AsyncTask
它从 Internet 下载一些文件。
private class DownloadImgs extends AsyncTask<Void, String, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
@Override
protected Void doInBackground(Void ...params) {
for(GetCountry gc : listCountry){
getdownloadedFiles(gc.getUrl());}
return null;
}
protected void onProgressUpdate(String... progress) {
pDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(Void result) {
dismissDialog(progress_bar_type);
}
允许我下载文件的方法:
public void getdownloadedFiles(String url)
{
int count;
try {
URL urlImg = new URL(url);
URLConnection connection = urlImg.openConnection();
connection.connect();
int lenghtOfFile = connection.getContentLength();
InputStream input = new BufferedInputStream(urlImg.openStream(), 8192);
OutputStream output = new FileOutputStream(folder+"/"+name);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
e.getMessage();
}
}
这段代码运行良好,但问题是每个下载的文件都有自己的进度条我想要所有下载的文件只有一个进度条。
我怎样才能做到这一点?非常感谢