我想检查一个线程是否完成。我想下载一些数据,当下载完成然后想要烘烤完成的消息下载。这是我的代码,我在点击方法上使用btn启动download.Now我想检查下载是否完成。
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);
}
答案 0 :(得分:2)
为此使用AsyncTask()。它比线程
更好DownloadAsync download = new DownloadAsync(position);
download.execute();
private class DownloadAsync extends AsyncTask<Void, Void, Void> {
int pos;
public incidentDetailAsync(int position) {
pos = position;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
try {
progressDilaog = ProgressDialog.show(ActivityA.this,
"", "Loading", true, false);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected Void doInBackground(Void... params) {
try {
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) {
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progressDilaog.dismiss();
Toast.makeText(ActivityA.this,
"Successfully Download", Toast.LENGTH_LONG).show();
}
}
答案 1 :(得分:0)
您可以使用处理程序来实现此目的。 有关详细信息,请参阅此link。
答案 2 :(得分:0)
最简单的方法是使用http://developer.android.com/reference/android/os/AsyncTask.html。将要运行的代码放在doInBackground中的单独线程上。在onPostExecute中单独的线程完成后,将要运行的代码放在UI线程上。
答案 3 :(得分:0)
使用:new AsyncAction().execute(null,null,null);
private class AsyncAction extends AsyncTask<String, Void, String>
{
public boolean status=false;
private ProgressDialog pd;
@Override
protected String doInBackground(String... arg0)
{
// TODO Auto-generated method stub
try
{
// download code
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) {
}
}
status=true;
}
catch (Exception e)
{
// TODO: handle exception
}
return null;
}
@Override
protected void onPostExecute(String result)
{
pd.dismiss();
Toast.makeText(ActivityA.this,
"Successfully Download", Toast.LENGTH_LONG).show();
}
protected void onPreExecute()
{
// TODO Auto-generated method stub
super.onPreExecute();
pd = new ProgressDialog(ticket_detail.this);
pd.setMessage("Please Wait ...");
pd.setIndeterminate(true);
pd.setCancelable(false);
pd.show();
}
}