Android AsyncTask实现一个线程操作完成后启动另一个线程

本文通过一个示例展示了如何使用Android的AsyncTask在下载一张图片完成后,依次启动新的任务下载下一张图片。每个AsyncTask负责下载并显示一张图片,利用onPostExecute方法触发下一次下载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何在一个线程完成操作后执行另一个线程?

有时候我们需要等待一个线程执行完成后再执行下一个线程。

发现asynctask可以实现这个功能,可以在一个线程操作完成后执行下一个线程。

 

原理就不多说了,直接看代码:

 

 实现下载三个网络图片,第一幅图片下载完成后接着下载第二个图片,第二个图片下载完成后接着下载第三个图片。

public class AsyncTaskTest extends Activity {

private Button button01;

private ImageView imageView01,imageView02,imageView03;

private ProgressDialog progressDialog01,progressDialog02,progressDialog03;

 

private String url01="http://img2.3lian.com/img2007/19/33/025.jpg";

private String url02="http://img2.3lian.com/img2007/19/33/026.jpg";

private String url03="http://img2.3lian.com/img2007/19/33/027.jpg";

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.async_task);

button01=(Button)findViewById(R.id.button01);

imageView01=(ImageView)findViewById(R.id.imageview01);

imageView02=(ImageView)findViewById(R.id.imageview02);

imageView03=(ImageView)findViewById(R.id.imageview03);

progressDialog01=new ProgressDialog(this);

progressDialog02=new ProgressDialog(this);

progressDialog03=new ProgressDialog(this);

button01.setOnClickListener(new OnClickListener() {

 

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

DownloadImagTask01 task01=new DownloadImagTask01();

task01.execute(url01);

}        

});

 

 

}

 

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.async_task_test, menu);

return true;

}

 

private class DownloadImagTask01 extends AsyncTask<String, Void, Bitmap>{

 

@Override

protected void onPreExecute() {

// TODO Auto-generated method stub

super.onPreExecute();

            progressDialog01.setTitle("图片01下载提示");

progressDialog01.setMessage("图片01正在下载中");

progressDialog01.show();

}

@Override

protected Bitmap doInBackground(String... params) {

// TODO Auto-generated method stub

Bitmap bitmap=null;

try {

URL url=new URL(params[0]);

HttpURLConnection connection=(HttpURLConnection)url.openConnection();

connection.setDoInput(true);

connection.connect();

InputStream inputStream=connection.getInputStream();

bitmap=BitmapFactory.decodeStream(inputStream);

inputStream.close();

 

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 

 

return bitmap;

}

                       

protected void onPostExecute(Bitmap result) {

// TODO Auto-generated method stub

super.onPostExecute(result);

imageView01=(ImageView)findViewById(R.id.imageview01);

imageView01.setImageBitmap(result);

DownloadImagTask02 task02=new DownloadImagTask02();

progressDialog01.dismiss();

task02.execute(url02);

}

 

 

 

}        

 

 

private class DownloadImagTask02 extends AsyncTask<String, Void, Bitmap>{

 

@Override

protected void onPreExecute() {

// TODO Auto-generated method stub

super.onPreExecute();

progressDialog02.setTitle("图片02下载提示");

progressDialog02.setMessage("图片02正在下载中");

progressDialog02.show();

}

@Override

protected Bitmap doInBackground(String... params) {

// TODO Auto-generated method stub

Bitmap bitmap=null;

try {

URL url=new URL(params[0]);

HttpURLConnection connection=(HttpURLConnection)url.openConnection();

connection.setDoInput(true);

connection.connect();

InputStream inputStream=connection.getInputStream();

bitmap=BitmapFactory.decodeStream(inputStream);

inputStream.close();

 

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 

 

return bitmap;

}

                       

protected void onPostExecute(Bitmap result) {

// TODO Auto-generated method stub

super.onPostExecute(result);

imageView02.setImageBitmap(result);

progressDialog02.dismiss();

DownloadImagTask03 task03=new DownloadImagTask03();

task03.execute(url03);

}

 

 

 

}        

 

private class DownloadImagTask03 extends AsyncTask<String, Void, Bitmap>{

 

@Override

protected void onPreExecute() {

// TODO Auto-generated method stub

super.onPreExecute();

progressDialog03.setTitle("图片03下载提示");

progressDialog03.setMessage("图片03正在下载中");

progressDialog03.show();

}

@Override

protected Bitmap doInBackground(String... params) {

// TODO Auto-generated method stub

Bitmap bitmap=null;

try {

URL url=new URL(params[0]);

HttpURLConnection connection=(HttpURLConnection)url.openConnection();

connection.setDoInput(true);

connection.connect();

InputStream inputStream=connection.getInputStream();

bitmap=BitmapFactory.decodeStream(inputStream);

inputStream.close();

 

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 

 

return bitmap;

}

                       

protected void onPostExecute(Bitmap result) {

// TODO Auto-generated method stub

super.onPostExecute(result);

imageView03.setImageBitmap(result);

progressDialog03.dismiss();

}

 

 

 

}        

 

 

}

 

布局文件内容为:

<ScrollView 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">

<LinearLayout

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:orientation="vertical"

 >

<Button

    android:id="@+id/button01"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="开始下载图片"

    />

    <ImageView

        android:id="@+id/imageview01"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

         />

    <ImageView

        android:id="@+id/imageview02"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

         />

    <ImageView

        android:id="@+id/imageview03"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

         />

 

</LinearLayout>

</ScrollView>

 

 

注意添加网络权限

 

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值