我有一个程序,点击一个按钮,点击时,4个下载应同时执行。我用ASyncTask
类此目的,for
迭代:几次同时使用一个ASyncTask
for(int i=0;i<downloadCounts;i++){
new DownloadTask().execute(url[i]);
}
但在运行时,只有一个下载执行,所有4个progressbars表明,单次下载。 我想同时下载4次下载。我能怎么做?
有关更多详细信息,我的下载管理器,根据文件大小获取链接并将其分成4个块。然后用上述for
迭代器,命令它运行的4个部分下载这个类:
private class DownloadChunks extends AsyncTask<Long,String,String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
setStatusText(-1);
}
@Override
protected String doInBackground(Long... params) {
long s1 = params[0];
long s2 = params[1];
int count;
try{
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Range", "bytes=" + s1 + "-" + s2);
connection.connect();
len2 = connection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(),8192);
File file = new File(Environment.getExternalStorageDirectory()+"/nuhexxxx");
if(!file.exists())file.mkdirs();
OutputStream output = new FileOutputStream(file+"/nuhe1.mp3");
byte[] data = new byte[1024];
long total = 0;
while ((count= input.read(data))!=-1){
total += count;
publishProgress(""+(int)((total*100)/len2));
output.write(data,0,count);
}
output.flush();
output.close();
input.close();
counter++;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
setStatusText(Integer.parseInt(values[0]));
}
@Override
protected void onPostExecute(String aVoid) {
super.onPostExecute(aVoid);
Log.e("This part is downloaded", "..." + len2 + " start with: " + counter);
}
}
所有日志表明,每一件事情是确定,文件下载完毕。但是每个块下载都是分开的和按顺序的。我想下载块同时
检查的详细信息根据您的代码,在我看来,应该开始四个下载。但是,请张贴更多的代码,以便我们提供帮助。 –
@RuchiraRandana更多详细信息增加 –