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";
@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);
}
}
}
運行結果
先后順序分別是下載前,下載中和下載后
總結
關于怎么使用看文檔和這個例子就夠了,下面說下,使用時的注意事項:
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。