public abstract class AsyncTask<Params, Progress, Result>
public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,
Params... params) {
if (mStatus != Status.PENDING) {
switch (mStatus) {
case RUNNING:
throw new IllegalStateException("Cannot execute task:"
+ " the task is already running.");
case FINISHED:
throw new IllegalStateException("Cannot execute task:"
+ " the task has already been executed "
+ "(a task can be executed only once)");
}
}
... ...所以你希望多次调用的话,只能new多个AsyncTask了,像这样:new AsyncTask(){}.execute(..);
new AsyncTask<Void, Void, Void>() {
protected Void doInBackground(Void... params)
{
//TODO handle task
return null;
}
@Override
protected void onPostExecute(Void result)
{
//TODO update UI
}
}.execute(null);
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1000" />
<Button
android:id="@+id/but_down"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下载" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/pb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_gravity="center" />
<TextView
android:id="@+id/tv_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#fff"
android:visibility="gone"
android:text="下载进度:"
android:textColor="#000" />
</LinearLayout>
</FrameLayout>activity:package com.example.asynctaskdemo4;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener
{
private static final String PATH = "http://10.46.191.198:8080/demo.bmp";
private Button but_down = null;
private ImageView iv_show = null;
private TextView tv_progress = null;
private ProgressBar pb = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but_down = (Button) findViewById(R.id.but_down);
iv_show = (ImageView) findViewById(R.id.iv_show);
tv_progress = (TextView) findViewById(R.id.tv_progress);
pb = (ProgressBar) findViewById(R.id.pb);
but_down.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if (R.id.but_down == v.getId())
{
new DownloadImageTask().execute(PATH);
}
}
private class DownloadImageTask extends AsyncTask<String, Integer, Bitmap>
{
@Override
protected void onPostExecute(Bitmap result)
{
if (result != null)
{
tv_progress.setVisibility(View.GONE);
pb.setVisibility(View.GONE);
iv_show.setImageBitmap(result);
} else
{
tv_progress.setVisibility(View.GONE);
pb.setVisibility(View.GONE);
Toast.makeText(MainActivity.this,"下载失败",0).show();
}
}
@Override
protected Bitmap doInBackground(String... params)
{
String path = params[0];
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(path);
try
{
HttpResponse resp = client.execute(get);
if(resp.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = resp.getEntity();
if(entity == null)
{
return null;
}
long total_length = entity.getContentLength();//获取文件总长
InputStream is = entity.getContent();
ByteArrayOutputStream bous = new ByteArrayOutputStream();
int len = 0;
byte[] buf = new byte[1024];
int current_len = 0;
int progress = 0;//当前下载进度
while((len = is.read(buf))!= -1)
{
current_len+=len;
bous.write(buf, 0, len);
//注意progress的写法哦
progress = (int) ((current_len/(float)total_length)*100);
this.publishProgress(progress);
}
is.close();
byte[] data = bous.toByteArray();
Options opts = new Options();
opts.inSampleSize = 2;//简单起见直接指定缩放比例
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
return bitmap;
}
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values)
{
tv_progress.setText("下载进度:"+values[0]);
}
@Override
protected void onPreExecute()
{
tv_progress.setVisibility(View.VISIBLE);
pb.setVisibility(View.VISIBLE);
}
}
}显示效果:原文:http://blog.csdn.net/chdjj/article/details/19913719