我试图用各种for循环下载几个png文件。请问在我的情况下,我如何使用publishProgress()方法?如何计算下载多个文件的AsyncTask的进度
这里是我的代码:
protected String doInBackground(String... params) {
for(PlaceDetails place : places){
for(int v = 14; v <=16; v++){
for (int y = 0; y <= placeBottomLeft.getYTile(); y++){
for(int x = placeTopLeft.getXTile(); x <= placeBottomRight.getXTile(); x++){
try {
//Download some stuff here
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
for (int w = zoom -1 ; w <= zoom+1; w++){
for (int y = topLeft.getYTile(); y <= bottomLeft.getYTile(); y++){
for(int x = topLeft.getXTile(); x <= bottomRight.getXTile(); x++){
try {
//Download some stuff here again
} catch (Exception e) {
Log.e("URL::: ERROR", e.getMessage());
e.printStackTrace();
}
}
}
}
return null;
}
正如你所看到的,我想下载的东西,在2个独立的在doInBackground方法循环。然而,我见过的大多数例子只是让线程休眠,并将for循环计数器号用作publishProgress()方法的整数。在这种情况下我应该如何使用它?谢谢!
编辑:
为了进一步澄清,我想知道是否有可能有最新进展情况,包括事情在onPostExecuteMethod()完成。
protected void onPostExecute (String result){
File newFileDir = new File(Environment.getExternalStorageDirectory().toString() + "/Qiito Offline/offlineMap");
File fileDir = new File(Environment.getExternalStorageDirectory().toString()
+ "/test");
try {
Log.e("Starting :: ", newFileDir.getPath());
File newFile = new File(newFileDir, "" + tID + ".zip");
newFileDir.mkdirs();
OutputStream output = new FileOutputStream(newFile);
ZipOutputStream zipoutput = new ZipOutputStream(output);
zip(fileDir, fileDir, zipoutput);
zipoutput.close();
output.close();
deleteDirectory(fileDir);
mNotificationHelper.completed(); //method I used for my NotificationHelper to inform that the entire process is completed
} catch (IOException excep) {
Log.e("ERROR!!" , excep.getLocalizedMessage());
}
}
在postExecute()中,我试图压缩另一个文件夹中的png文件,然后删除所有的png文件。进度条是否也可以包含这一点?否则,我只会跟踪下载png文件。
你想分开显示不同文件中的所有文件一起进步或者进步? –
如果可能,我想直接从onPreExecute()开始显示它的进度,直到在onPostExecute()完成为止。 – lyk