如何检查android中已完成的线程?
我想检查一个线程是否完成。我想下载一些数据,当下载完成后,我想发送一条消息“下载完成”。这是我的代码,我使用btn on click方法开始下载。现在我想检查下载是否完成如何检查android中已完成的线程?,android,multithreading,download,Android,Multithreading,Download,我想检查一个线程是否完成。我想下载一些数据,当下载完成后,我想发送一条消息“下载完成”。这是我的代码,我使用btn on click方法开始下载。现在我想检查下载是否完成 public void startDownload(final int position) { Runnable runnable = new Runnable() { int Status = 0; public void run() {
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);
}
为此使用AYSNTASK()。它比线好
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();
}
}
downloadsync download=new downloadsync(位置);
下载.execute();
私有类下载异步扩展异步任务{
int pos;
公共事件详细信息异步(内部位置){
pos=位置;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
试一试{
progressDilaog=ProgressDialog.show(ActivityA.this,
“,”加载“,对,错);
}捕获(例外e){
e、 printStackTrace();
}
}
@凌驾
受保护的Void doInBackground(Void…参数){
试一试{
字符串urlDownload=MyArrList.get(位置)
.get(“VideoPathThum”).toString();
Log.v(“Log_标记”,“urlDownload::”+urlDownload
+“位置开始下载::”+位置);
整数计数=0;
试一试{
URL=新URL(URL下载);
URLConnection conexion=url.openConnection();
conexion.connect();
int lenghtOfFile=conexion.getContentLength();
//Log.d(“ANDRO_ASYNC”,“文件长度:“+lenghtOfFile”);
InputStream输入=新的BufferedInputStream(
openStream());
//从URL获取文件名
字符串文件名=urlDownload.substring(
urlDownload.lastIndexOf('/')+1,
urlDownload.length());
下载=新文件(
Environment.getExternalStorageDirectory()
+“/下载/”;
如果(!download.exists()){
下载.mkdir();
}
strDownloaDuRL=下载+“/”+文件名;
OutputStream output=新文件OutputStream(strDownloaDuRL);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
if(Thread.interrupted()==true){
Log.v(“日志标签”、“中断”);
output.flush();
output.close();
input.close();
更新状态(位置,0);
SetMainProgressbar();
打破
}
总数+=计数;
状态=(整数)((总计*100)/长度文件);
输出.写入(数据,0,计数);
TestScrollNewDownloadActivity.this
.runOnUiThread(新的Runnable(){
公开募捐{
更新状态(位置、状态);
SetMainProgressbar();
//BusyExtMemory();
}
});
}
output.flush();
output.close();
input.close();
}捕获(例外e){
}
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
进行,进行;进行;进行;
Toast.makeText(活动a.this,
“下载成功”,Toast.LENGTH_LONG.show();
}
}
最简单的方法是使用。将要运行的代码放在doInBackground中的单独线程上。在onPostExecute中的独立线程完成后,将要运行的代码放在UI线程上。使用
调用时使用:new AsyncAction().execute(null,null,null)代码>
私有类AsyncAction扩展了AsyncTask
{
公共布尔状态=false;
私营部门;
@凌驾
受保护的字符串doInBackground(字符串…arg0)
{
//TODO自动生成的方法存根
尝试
{
//下载代码
字符串urlDownload=MyArrList.get(位置)
.get(“VideoPathThum”).toString();
Log.v(“Log_标记”,“urlDownload::”+urlDownload
+“位置开始下载::”+位置);
整数计数=0;
试一试{
URL=新URL(URL下载);
URLConnection conexion=url.openConnection();
conexion.connect();
int lenghtOfFile=conexion.getContentLength();
//Log.d(“ANDRO_ASYNC”,“文件长度:“+lenghtOfFile”);
InputStream输入=新的BufferedInputStream(
openStream());
//从URL获取文件名
字符串文件名=urlDownload.substring(
urlDownload.lastIndexOf('/')+1,
urlDownload.length());
下载=新文件(
Environment.getExternalStorageDirectory()
+“/下载/”;
如果(!download.exists()){
下载.mkdir();
}
strDownloaDuRL=下载+“/”+文件名;
OutputStream output=新文件OutputStream(strDownloaDuRL);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
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();
}
}