Android asynctask等待另一个asynctask

时间:2014-03-18 13:09:14

标签: android android-asynctask

  public class GetFlyers extends AsyncTask<String, Void, Bitmap> {
ImageView imageView;
int imageSize;
Context context;
String url="";
String im="";
Flyer fly;
ImageFragment i1;
int position;
public GetFlyers(ImageView imageView, int imageSize, Context conext,String im, Flyer flu, ImageFragment i1, int position )
{
    this.im = im;
    this.position = position;
    this.imageView = imageView;
    this.imageSize = imageSize;
    this.context=context;
    this.i1 = i1;
    fly = flu;
}
protected Bitmap doInBackground(String... urls) {
    Bitmap map = null;
    for (String url : urls) {
        Log.d("firste","image loaded back");
        if(url != null && !url.trim().equals(""))
        {
            map = downloadImage(url);
            this.url = url;
        }
    }
    return map;
}
@Override
protected void onPostExecute(Bitmap result) {   }
private Bitmap downloadImage(String url) {
    Bitmap bitmap = null;
    InputStream stream = null;
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inSampleSize = 1;
    try {
        stream = getHttpConnection(url);
        bitmap = BitmapFactory.
                decodeStream(stream, null, bmOptions);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    finally
    {
        try
        {
            stream.close();
        }
        catch(Exception e)
        {
        }
    }

    return bitmap;
}
// Makes HttpURLConnection and returns InputStream
private InputStream getHttpConnection(String urlString)
        throws IOException {
    InputStream stream = null;
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();
    try {
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        httpConnection.setRequestMethod("GET");
        httpConnection.connect();
        if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            stream = httpConnection.getInputStream();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return stream;
}

}

我有这个代码,每当我调用这个帖子时......让我说3次.. GetFlyers task = new GetFlyers tasl.execute ...遗憾的是,每次拨打一个电话。只有一个asynctask去..我想在运行这个

时运行另一个asynctask

1 个答案:

答案 0 :(得分:3)

AsyncTasks默认从Honeycomb开始按顺序执行。

要在所有platrorms上并行运行AsyncTasks,请以

运行
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
    task.execute();
}

AsyncTask: Order of Execution

AsyncTask.executeOnExecutor(Executor, Params)