AsyncTask android多線程

Understanding AsyncTask

AsyncTask是Android 1.5 Cubake加入的用於實現異步操作的一個類,在此之前只能用Java SE庫中的Thread來實現多線程異步,AsyncTask是Android平臺自己的異步工具,融入了Android平臺的特性,讓異步操作更加的安全,方便和實用。實質上它也是對Java SE庫中Thread的一個封裝,加上了平臺相關的特性,所以對於所有的多線程異步都強烈推薦使用AsyncTask,因爲它考慮,也融入了Android平臺的特性,更加的安全和高效。

AsyncTask可以方便的執行異步操作(doInBackground),又能方便的與主線程進行通信,它本身又有良好的封裝性,可以進行取消操作(cancel())。關於AsyncTask的使用,文檔說的很明白,下面直接上實例。

實例

這個實例用AsyncTask到網絡上下載圖片,同時顯示進度,下載完圖片更新UI。

  1. package com.hilton.effectiveandroid.concurrent;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.io.OutputStream;  
  6. import java.net.HttpURLConnection;  
  7. import java.net.MalformedURLException;  
  8. import java.net.URL;  
  9.   
  10. import android.app.Activity;  
  11. import android.content.Context;  
  12. import android.graphics.Bitmap;  
  13. import android.graphics.BitmapFactory;  
  14. import android.os.AsyncTask;  
  15. import android.os.Bundle;  
  16. import android.os.SystemClock;  
  17. import android.view.View;  
  18. import android.widget.Button;  
  19. import android.widget.ImageView;  
  20. import android.widget.ProgressBar;  
  21.   
  22. import com.hilton.effectiveandroid.R;  
  23.   
  24. /* 
  25.  * AsyncTask cannot be reused, i.e. if you have executed one AsyncTask, you must discard it, you cannot execute it again. 
  26.  * If you try to execute an executed AsyncTask, you will get "java.lang.IllegalStateException: Cannot execute task: the task is already running" 
  27.  * In this demo, if you click "get the image" button twice at any time, you will receive "IllegalStateException". 
  28.  * About cancellation: 
  29.  * You can call AsyncTask#cancel() at any time during AsyncTask executing, but the result is onPostExecute() is not called after 
  30.  * doInBackground() finishes, which means doInBackground() is not stopped. AsyncTask#isCancelled() returns true after cancel() getting 
  31.  * called, so if you want to really cancel the task, i.e. stop doInBackground(), you must check the return value of isCancelled() in 
  32.  * doInBackground, when there are loops in doInBackground in particular. 
  33.  * This is the same to Java threading, in which is no effective way to stop a running thread, only way to do is set a flag to thread, and check 
  34.  * the flag every time in Thread#run(), if flag is set, run() aborts. 
  35.  */  
  36. public class AsyncTaskDemoActivity extends Activity {  
  37.     private static final String ImageUrl = "http://i1.cqnews.net/sports/attachement/jpg/site82/2011-10-01/2960950278670008721.jpg";  
  38.     private ProgressBar mProgressBar;  
  39.     private ImageView mImageView;  
  40.     private Button mGetImage;  
  41.     private Button mAbort;  
  42.       
  43.     @Override  
  44.     public void onCreate(Bundle icicle) {  
  45.     super.onCreate(icicle);  
  46.     setContentView(R.layout.async_task_demo_activity);  
  47.     mProgressBar = (ProgressBar) findViewById(R.id.async_task_progress);  
  48.     mImageView = (ImageView) findViewById(R.id.async_task_displayer);  
  49.     final ImageLoader loader = new ImageLoader();  
  50.     mGetImage = (Button) findViewById(R.id.async_task_get_image);  
  51.     mGetImage.setOnClickListener(new View.OnClickListener() {  
  52.         public void onClick(View v) {  
  53.         loader.execute(ImageUrl);  
  54.         }  
  55.     });  
  56.     mAbort = (Button) findViewById(R.id.asyc_task_abort);  
  57.     mAbort.setOnClickListener(new View.OnClickListener() {  
  58.         public void onClick(View v) {  
  59.         loader.cancel(true);  
  60.         }  
  61.     });  
  62.     mAbort.setEnabled(false);  
  63.     }  
  64.       
  65.     private class ImageLoader extends AsyncTask<String, Integer, Bitmap> {  
  66.     private static final String TAG = "ImageLoader";  
  67.   
  68.     @Override  
  69.     protected void onPreExecute() {  
  70.         // Initialize progress and image  
  71.         mGetImage.setEnabled(false);  
  72.         mAbort.setEnabled(true);  
  73.         mProgressBar.setVisibility(View.VISIBLE);  
  74.         mProgressBar.setProgress(0);  
  75.         mImageView.setImageResource(R.drawable.icon);  
  76.     }  
  77.       
  78.     @Override  
  79.     protected Bitmap doInBackground(String... url) {  
  80.         /* 
  81.          * Fucking ridiculous thing happened here, to use any Internet connections, either via HttpURLConnection 
  82.          * or HttpClient, you must declare INTERNET permission in AndroidManifest.xml. Otherwise you will get 
  83.          * "UnknownHostException" when connecting or other tcp/ip/http exceptions rather than "SecurityException" 
  84.          * which tells you need to declare INTERNET permission. 
  85.          */  
  86.         try {  
  87.         URL u;  
  88.         HttpURLConnection conn = null;  
  89.         InputStream in = null;  
  90.         OutputStream out = null;  
  91.         final String filename = "local_temp_image";  
  92.         try {  
  93.             u = new URL(url[0]);  
  94.             conn = (HttpURLConnection) u.openConnection();  
  95.             conn.setDoInput(true);  
  96.             conn.setDoOutput(false);  
  97.             conn.setConnectTimeout(20 * 1000);  
  98.             in = conn.getInputStream();  
  99.             out = openFileOutput(filename, Context.MODE_PRIVATE);  
  100.             byte[] buf = new byte[8196];  
  101.             int seg = 0;  
  102.             final long total = conn.getContentLength();  
  103.             long current = 0;  
  104.             /* 
  105.              * Without checking isCancelled(), the loop continues until reading whole image done, i.e. the progress 
  106.              * continues go up to 100. But onPostExecute() will not be called. 
  107.              * By checking isCancelled(), we can stop immediately, i.e. progress stops immediately when cancel() is called. 
  108.              */  
  109.             while (!isCancelled() && (seg = in.read(buf)) != -1) {  
  110.             out.write(buf, 0, seg);  
  111.             current += seg;  
  112.             int progress = (int) ((float) current / (float) total * 100f);  
  113.             publishProgress(progress);  
  114.             SystemClock.sleep(1000);  
  115.             }  
  116.         } finally {  
  117.             if (conn != null) {  
  118.             conn.disconnect();  
  119.             }  
  120.             if (in != null) {  
  121.             in.close();  
  122.             }  
  123.             if (out != null) {  
  124.             out.close();  
  125.             }  
  126.         }  
  127.         return BitmapFactory.decodeFile(getFileStreamPath(filename).getAbsolutePath());  
  128.         } catch (MalformedURLException e) {  
  129.         e.printStackTrace();  
  130.         } catch (IOException e) {  
  131.         e.printStackTrace();  
  132.         }  
  133.         return null;  
  134.     }  
  135.       
  136.     @Override  
  137.     protected void onProgressUpdate(Integer... progress) {  
  138.         mProgressBar.setProgress(progress[0]);  
  139.     }  
  140.       
  141.     @Override  
  142.     protected void onPostExecute(Bitmap image) {  
  143.         if (image != null) {  
  144.         mImageView.setImageBitmap(image);  
  145.         }  
  146.         mProgressBar.setProgress(100);  
  147.         mProgressBar.setVisibility(View.GONE);  
  148.         mAbort.setEnabled(false);  
  149.     }  
  150.     }  
  151. }  
運行結果

先後順序分別是下載前,下載中和下載後

總結

關於怎麼使用看文檔和這個例子就夠了,下面說下,使用時的注意事項:

1. AsyncTask對象不可重複使用,也就是說一個AsyncTask對象只能execute()一次,否則會有異常拋出"java.lang.IllegalStateException: Cannot execute task: the task is already running"
2. 在doInBackground()中要檢查isCancelled()的返回值,如果你的異步任務是可以取消的話。
cancel()僅僅是給AsyncTask對象設置了一個標識位,當調用了cancel()後,發生的事情只有:AsyncTask對象的標識位變了,和doInBackground()執行完成後,onPostExecute()不會被回調了,而doInBackground()和onProgressUpdate()還是會繼續執行直到doInBackground()結束。所以要在doInBackground()中不斷的檢查isCancellled()的返回值,當其返回true時就停止執行,特別是有循環的時候。如上面的例子,如果把讀取數據的isCancelled()檢查去掉,圖片還是會下載,進度也一直會走,只是最後圖片不會放到UI上(因爲onPostExecute()沒被回調)!
這裏的原因其實很好理解,想想Java SE的Thread吧,是沒有方法將其直接Cacncel掉的,那些線程取消也無非就是給線程設置標識位,然後在run()方法中不斷的檢查標識而已。

3. 如果要在應用程序中使用網絡,一定不要忘記在AndroidManifest中聲明INTERNET權限,否則會報出很詭異的異常信息,比如上面的例子,如果把INTERNET權限拿掉會拋出"UnknownHostException"。剛開始很疑惑,因爲模擬器是可以正常上網的,後來Google了下才發現原來是沒權限,但是疑問還是沒有消除,既然沒有聲明網絡權限,爲什麼不直接提示無網絡權限呢?

對比Java SE的Thread

Thread是非常原始的類,它只有一個run()方法,一旦開始,無法停止,它僅適合於一個非常獨立的異步任務,也即不需要與主線程交互,對於其他情況,比如需要取消或與主線程交互,都需添加額外的代碼來實現,並且還要注意同步的問題。

而AsyncTask是封裝好了的,可以直接拿來用,如果你僅執行獨立的異步任務,可以僅實現doInBackground()。

所以,當有一個非常獨立的任務時,可以考慮使用Thread,其他時候,儘可能的用AsyncTask。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章