I want to check whether a thread is completed or not.I want to download some data, when the download is completed then want to toast a message download completed .Here is my code and I used btn on click method to start the download.Now I want to check whether the download is completed.
public void startDownload(final int position) {
Runnable runnable = new Runnable() {
int Status = 0;
public void run() {
Log.v("thread", "thread rtun ");
String urlDownload = MyArrList.get(position)
.get("VideoPathThum").toString();
Log.v("log_tag", "urlDownload ::: " + urlDownload
+ "Position StartDownload ::: " + position);
int count = 0;
try {
URL url = new URL(urlDownload);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
// Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(
url.openStream());
// Get File Name from URL
String fileName = urlDownload.substring(
urlDownload.lastIndexOf('/') + 1,
urlDownload.length());
download = new File(
Environment.getExternalStorageDirectory()
+ "/download/");
if (!download.exists()) {
download.mkdir();
}
strDownloaDuRL = download + "/" + fileName;
OutputStream output = new FileOutputStream(strDownloaDuRL);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
if (Thread.interrupted() == true) {
Log.v("log_tag", " interrupt");
output.flush();
output.close();
input.close();
updateStatus(position, 0);
SetMainProgressbar();
break;
}
total += count;
Status = (int) ((total * 100) / lenghtOfFile);
output.write(data, 0, count);
TestScrollNewDownloadActivity.this
.runOnUiThread(new Runnable() {
public void run() {
updateStatus(position, Status);
SetMainProgressbar();
// BusyExtMemory();
}
});
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
}
}
};
tr = new Thread(runnable);
tr.start();
trlist.set(position, tr);
}