Android開發(fā)筆記之:深入理解多線程AsyncTask
更新時間:2013年05月21日 10:15:17 作者:
本篇文章是對Android中多線程AsyncTask進行了詳細的分析介紹,需要的朋友參考下
Understanding AsyncTask
AsyncTask是Android 1.5 Cubake加入的用于實現(xiàn)異步操作的一個類,在此之前只能用Java SE庫中的Thread來實現(xiàn)多線程異步,AsyncTask是Android平臺自己的異步工具,融入了Android平臺的特性,讓異步操作更加的安全,方便和實用。實質(zhì)上它也是對Java SE庫中Thread的一個封裝,加上了平臺相關(guān)的特性,所以對于所有的多線程異步都強烈推薦使用AsyncTask,因為它考慮,也融入了Android平臺的特性,更加的安全和高效。
AsyncTask可以方便的執(zhí)行異步操作(doInBackground),又能方便的與主線程進行通信,它本身又有良好的封裝性,可以進行取消操作(cancel())。關(guān)于AsyncTask的使用,文檔說的很明白,下面直接上實例。
實例
這個實例用AsyncTask到網(wǎng)絡(luò)上下載圖片,同時顯示進度,下載完圖片更新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";
@Override
protected void onPreExecute() {
// Initialize progress and image
mGetImage.setEnabled(false);
mAbort.setEnabled(true);
mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.setProgress(0);
mImageView.setImageResource(R.drawable.icon);
}
@Override
protected 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;
}
@Override
protected void onProgressUpdate(Integer... progress) {
mProgressBar.setProgress(progress[0]);
}
@Override
protected void onPostExecute(Bitmap image) {
if (image != null) {
mImageView.setImageBitmap(image);
}
mProgressBar.setProgress(100);
mProgressBar.setVisibility(View.GONE);
mAbort.setEnabled(false);
}
}
}
運行結(jié)果
總結(jié)
關(guān)于怎么使用看文檔和這個例子就夠了,下面說下,使用時的注意事項:
1. AsyncTask對象不可重復(fù)使用,也就是說一個AsyncTask對象只能execute()一次,否則會有異常拋出"java.lang.IllegalStateException: Cannot execute task: the task is already running"
2. 在doInBackground()中要檢查isCancelled()的返回值,如果你的異步任務(wù)是可以取消的話。
cancel()僅僅是給AsyncTask對象設(shè)置了一個標(biāo)識位,當(dāng)調(diào)用了cancel()后,發(fā)生的事情只有:AsyncTask對象的標(biāo)識位變了,和doInBackground()執(zhí)行完成后,onPostExecute()不會被回調(diào)了,而doInBackground()和onProgressUpdate()還是會繼續(xù)執(zhí)行直到doInBackground()結(jié)束。所以要在doInBackground()中不斷的檢查isCancellled()的返回值,當(dāng)其返回true時就停止執(zhí)行,特別是有循環(huán)的時候。如上面的例子,如果把讀取數(shù)據(jù)的isCancelled()檢查去掉,圖片還是會下載,進度也一直會走,只是最后圖片不會放到UI上(因為onPostExecute()沒被回調(diào))!
這里的原因其實很好理解,想想Java SE的Thread吧,是沒有方法將其直接Cacncel掉的,那些線程取消也無非就是給線程設(shè)置標(biāo)識位,然后在run()方法中不斷的檢查標(biāo)識而已。
3. 如果要在應(yīng)用程序中使用網(wǎng)絡(luò),一定不要忘記在AndroidManifest中聲明INTERNET權(quán)限,否則會報出很詭異的異常信息,比如上面的例子,如果把INTERNET權(quán)限拿掉會拋出"UnknownHostException"。剛開始很疑惑,因為模擬器是可以正常上網(wǎng)的,后來Google了下才發(fā)現(xiàn)原來是沒權(quán)限,但是疑問還是沒有消除,既然沒有聲明網(wǎng)絡(luò)權(quán)限,為什么不直接提示無網(wǎng)絡(luò)權(quán)限呢?
對比Java SE的Thread
Thread是非常原始的類,它只有一個run()方法,一旦開始,無法停止,它僅適合于一個非常獨立的異步任務(wù),也即不需要與主線程交互,對于其他情況,比如需要取消或與主線程交互,都需添加額外的代碼來實現(xiàn),并且還要注意同步的問題。
而AsyncTask是封裝好了的,可以直接拿來用,如果你僅執(zhí)行獨立的異步任務(wù),可以僅實現(xiàn)doInBackground()。
所以,當(dāng)有一個非常獨立的任務(wù)時,可以考慮使用Thread,其他時候,盡可能的用AsyncTask。
AsyncTask是Android 1.5 Cubake加入的用于實現(xiàn)異步操作的一個類,在此之前只能用Java SE庫中的Thread來實現(xiàn)多線程異步,AsyncTask是Android平臺自己的異步工具,融入了Android平臺的特性,讓異步操作更加的安全,方便和實用。實質(zhì)上它也是對Java SE庫中Thread的一個封裝,加上了平臺相關(guān)的特性,所以對于所有的多線程異步都強烈推薦使用AsyncTask,因為它考慮,也融入了Android平臺的特性,更加的安全和高效。
AsyncTask可以方便的執(zhí)行異步操作(doInBackground),又能方便的與主線程進行通信,它本身又有良好的封裝性,可以進行取消操作(cancel())。關(guān)于AsyncTask的使用,文檔說的很明白,下面直接上實例。
實例
這個實例用AsyncTask到網(wǎng)絡(luò)上下載圖片,同時顯示進度,下載完圖片更新UI。
復(fù)制代碼 代碼如下:
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";
@Override
protected void onPreExecute() {
// Initialize progress and image
mGetImage.setEnabled(false);
mAbort.setEnabled(true);
mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.setProgress(0);
mImageView.setImageResource(R.drawable.icon);
}
@Override
protected 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;
}
@Override
protected void onProgressUpdate(Integer... progress) {
mProgressBar.setProgress(progress[0]);
}
@Override
protected void onPostExecute(Bitmap image) {
if (image != null) {
mImageView.setImageBitmap(image);
}
mProgressBar.setProgress(100);
mProgressBar.setVisibility(View.GONE);
mAbort.setEnabled(false);
}
}
}
運行結(jié)果
總結(jié)
關(guān)于怎么使用看文檔和這個例子就夠了,下面說下,使用時的注意事項:
1. AsyncTask對象不可重復(fù)使用,也就是說一個AsyncTask對象只能execute()一次,否則會有異常拋出"java.lang.IllegalStateException: Cannot execute task: the task is already running"
2. 在doInBackground()中要檢查isCancelled()的返回值,如果你的異步任務(wù)是可以取消的話。
cancel()僅僅是給AsyncTask對象設(shè)置了一個標(biāo)識位,當(dāng)調(diào)用了cancel()后,發(fā)生的事情只有:AsyncTask對象的標(biāo)識位變了,和doInBackground()執(zhí)行完成后,onPostExecute()不會被回調(diào)了,而doInBackground()和onProgressUpdate()還是會繼續(xù)執(zhí)行直到doInBackground()結(jié)束。所以要在doInBackground()中不斷的檢查isCancellled()的返回值,當(dāng)其返回true時就停止執(zhí)行,特別是有循環(huán)的時候。如上面的例子,如果把讀取數(shù)據(jù)的isCancelled()檢查去掉,圖片還是會下載,進度也一直會走,只是最后圖片不會放到UI上(因為onPostExecute()沒被回調(diào))!
這里的原因其實很好理解,想想Java SE的Thread吧,是沒有方法將其直接Cacncel掉的,那些線程取消也無非就是給線程設(shè)置標(biāo)識位,然后在run()方法中不斷的檢查標(biāo)識而已。
3. 如果要在應(yīng)用程序中使用網(wǎng)絡(luò),一定不要忘記在AndroidManifest中聲明INTERNET權(quán)限,否則會報出很詭異的異常信息,比如上面的例子,如果把INTERNET權(quán)限拿掉會拋出"UnknownHostException"。剛開始很疑惑,因為模擬器是可以正常上網(wǎng)的,后來Google了下才發(fā)現(xiàn)原來是沒權(quán)限,但是疑問還是沒有消除,既然沒有聲明網(wǎng)絡(luò)權(quán)限,為什么不直接提示無網(wǎng)絡(luò)權(quán)限呢?
對比Java SE的Thread
Thread是非常原始的類,它只有一個run()方法,一旦開始,無法停止,它僅適合于一個非常獨立的異步任務(wù),也即不需要與主線程交互,對于其他情況,比如需要取消或與主線程交互,都需添加額外的代碼來實現(xiàn),并且還要注意同步的問題。
而AsyncTask是封裝好了的,可以直接拿來用,如果你僅執(zhí)行獨立的異步任務(wù),可以僅實現(xiàn)doInBackground()。
所以,當(dāng)有一個非常獨立的任務(wù)時,可以考慮使用Thread,其他時候,盡可能的用AsyncTask。
相關(guān)文章
Android實現(xiàn)設(shè)置APP灰白模式效果
大家好,本篇文章主要講的是Android實現(xiàn)設(shè)置APP灰白模式效果,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Android實現(xiàn)檢查并下載APK更新、安裝APK及獲取網(wǎng)絡(luò)信息的方法
這篇文章主要介紹了Android實現(xiàn)檢查并下載APK更新、安裝APK及獲取網(wǎng)絡(luò)信息的方法,很實用的功能,需要的朋友可以參考下2014-07-07Android Studio 3.5格式化布局代碼時錯位、錯亂bug的解決
這篇文章主要介紹了Android Studio 3.5格式化布局代碼時錯位、錯亂bug的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android實現(xiàn)錄音監(jiān)聽動畫的示例代碼
在很多app種內(nèi)置了語音助手,也存在各種動畫,這篇文章主要為大家詳細介紹了Android實現(xiàn)錄音監(jiān)聽動畫的示例代碼,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12Flutter?DateTime獲取本月的開始時間與結(jié)束時間方法
這篇文章主要為大家介紹了Flutter?DateTime獲取本月的開始時間與結(jié)束時間方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2023-05-05Android實現(xiàn)關(guān)機重啟的方法分享
這篇文章主要介紹了Android實現(xiàn)關(guān)機重啟的方法,需要的朋友可以參考下2014-02-02