Android實戰技巧:多線程AsyncTask

來源:互聯網
上載者:User
Understanding AsyncTask

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

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

執行個體

這個執行個體用AsyncTask到網路上下載圖片,同時顯示進度,下載完圖片更新UI。

package com.hilton.effectiveandroid.concurrent;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.AsyncTask;import android.os.Bundle;import android.os.SystemClock;import android.view.View;import android.widget.Button;import android.widget.ImageView;import android.widget.ProgressBar;import com.hilton.effectiveandroid.R;/* * AsyncTask cannot be reused, i.e. if you have executed one AsyncTask, you must discard it, you cannot execute it again. * If you try to execute an executed AsyncTask, you will get "java.lang.IllegalStateException: Cannot execute task: the task is already running" * In this demo, if you click "get the image" button twice at any time, you will receive "IllegalStateException". * About cancellation: * You can call AsyncTask#cancel() at any time during AsyncTask executing, but the result is onPostExecute() is not called after * doInBackground() finishes, which means doInBackground() is not stopped. AsyncTask#isCancelled() returns true after cancel() getting * called, so if you want to really cancel the task, i.e. stop doInBackground(), you must check the return value of isCancelled() in * doInBackground, when there are loops in doInBackground in particular. * 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 * the flag every time in Thread#run(), if flag is set, run() aborts. */public class AsyncTaskDemoActivity extends Activity {    private static final String ImageUrl = "http://i1.cqnews.net/sports/attachement/jpg/site82/2011-10-01/2960950278670008721.jpg";    private ProgressBar mProgressBar;    private ImageView mImageView;    private Button mGetImage;    private Button mAbort;        @Override    public void onCreate(Bundle icicle) {super.onCreate(icicle);setContentView(R.layout.async_task_demo_activity);mProgressBar = (ProgressBar) findViewById(R.id.async_task_progress);mImageView = (ImageView) findViewById(R.id.async_task_displayer);final ImageLoader loader = new ImageLoader();mGetImage = (Button) findViewById(R.id.async_task_get_image);mGetImage.setOnClickListener(new View.OnClickListener() {    public void onClick(View v) {loader.execute(ImageUrl);    }});mAbort = (Button) findViewById(R.id.asyc_task_abort);mAbort.setOnClickListener(new View.OnClickListener() {    public void onClick(View v) {loader.cancel(true);    }});mAbort.setEnabled(false);    }        private class ImageLoader extends AsyncTask<String, Integer, Bitmap> {private static final String TAG = "ImageLoader";@Overrideprotected void onPreExecute() {    // Initialize progress and image    mGetImage.setEnabled(false);    mAbort.setEnabled(true);    mProgressBar.setVisibility(View.VISIBLE);    mProgressBar.setProgress(0);    mImageView.setImageResource(R.drawable.icon);}@Overrideprotected Bitmap doInBackground(String... url) {    /*     * Fucking ridiculous thing happened here, to use any Internet connections, either via HttpURLConnection     * or HttpClient, you must declare INTERNET permission in AndroidManifest.xml. Otherwise you will get     * "UnknownHostException" when connecting or other tcp/ip/http exceptions rather than "SecurityException"     * which tells you need to declare INTERNET permission.     */    try {URL u;HttpURLConnection conn = null;InputStream in = null;OutputStream out = null;final String filename = "local_temp_image";try {    u = new URL(url[0]);    conn = (HttpURLConnection) u.openConnection();    conn.setDoInput(true);    conn.setDoOutput(false);    conn.setConnectTimeout(20 * 1000);    in = conn.getInputStream();    out = openFileOutput(filename, Context.MODE_PRIVATE);    byte[] buf = new byte[8196];    int seg = 0;    final long total = conn.getContentLength();    long current = 0;    /*     * Without checking isCancelled(), the loop continues until reading whole image done, i.e. the progress     * continues go up to 100. But onPostExecute() will not be called.     * By checking isCancelled(), we can stop immediately, i.e. progress stops immediately when cancel() is called.     */    while (!isCancelled() && (seg = in.read(buf)) != -1) {out.write(buf, 0, seg);current += seg;int progress = (int) ((float) current / (float) total * 100f);publishProgress(progress);SystemClock.sleep(1000);    }} finally {    if (conn != null) {conn.disconnect();    }    if (in != null) {in.close();    }    if (out != null) {out.close();    }}return BitmapFactory.decodeFile(getFileStreamPath(filename).getAbsolutePath());    } catch (MalformedURLException e) {e.printStackTrace();    } catch (IOException e) {e.printStackTrace();    }    return null;}@Overrideprotected void onProgressUpdate(Integer... progress) {    mProgressBar.setProgress(progress[0]);}@Overrideprotected void onPostExecute(Bitmap image) {    if (image != null) {mImageView.setImageBitmap(image);    }    mProgressBar.setProgress(100);    mProgressBar.setVisibility(View.GONE);    mAbort.setEnabled(false);}    }}

運行結果

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

總結

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

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。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.