我有imageurl數組,下載由DownloadFromUrl函數調用,myfunction調用,我使用線程,我不知道有多少圖像url,爲每個圖像下載單獨創建線程,我想在所有這些線程結束後開始一個活動。我如何檢查動態線程是否結束
我怎麼能得到所有這些線程,線程睡眠時間更長無法運行它不是一個好的程序。我也不能計算線程結束由靜態變量計數,因爲有時圖像無法下載或URL中斷,或連接不超時,
我現在有點迷路了,應該怎麼弄清楚這些所有線程結束?
public void DownloadFromUrl(String DownloadUrl, String fileName) {
try {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/"+Imageurl.facebookpage);
if(dir.exists()==false) {
dir.mkdirs();
}
URL url = new URL(DownloadUrl); //you can write here any link
File file = new File(dir, fileName);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(5000);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
LoginActivity.statsofdownload++;
Log.d("DownloadManager","file://"+file.getAbsolutePath());
} catch (IOException e) {
Imageurl.pagestat="space";
Log.d("DownloadManager", "Error: " + e);
}
}
myfunction()
{
for(String string : Imageurl.output) {
imagea++;
final int ind =imagea;
final String ss=string;
new Thread(new Runnable() {
public void run() {
DownloadFromUrl(ss,"IMAGE"+ind+".jpeg");
File root = android.os.Environment.getExternalStorageDirectory();
Imageurl.newyearsvalues.add("file://"+root.getAbsolutePath() + "/"+Imageurl.facebookpage+ "/"+"IMAGE"+ind+".jpeg");
}
}).start();
}
//// now need to call an activity but how I will know that these thread all end
}
好主意,thnaks – 2013-04-11 04:44:52