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

来源:互联网 发布:电气常用数据手册 编辑:程序博客网 时间:2024/05/16 10:25

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

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

发现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>

 

 

 

 

 

 

 

 

 

0 0