1. 程式人生 > >Android AsyncTask實現一個執行緒操作完成後啟動另一個執行緒

Android AsyncTask實現一個執行緒操作完成後啟動另一個執行緒

如何在一個執行緒完成操作後執行另一個執行緒?

有時候我們需要等待一個執行緒執行完成後再執行下一個執行緒。

發現asynctask可以實現這個功能,可以在一個執行緒操作完成後執行下一個執行緒。

原理就不多說了,直接看程式碼:

 實現下載三個網路圖片,第一幅圖片下載完成後接著下載第二個圖片,第二個圖片下載完成後接著下載第三個圖片。

public class AsyncTaskTest extends Activity {

private Button button01;

private ImageView imageView01,imageView02,imageView03;

private ProgressDialog progressDialog01,progressDialog02,progressDialog03;

@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>