understanding asynctask
asynctask是android 1.5 cubake加入的用于實(shí)現(xiàn)異步操作的一個(gè)類(lèi),在此之前只能用java se庫(kù)中的thread來(lái)實(shí)現(xiàn)多線程異步,asynctask是android平臺(tái)自己的異步工具,融入了android平臺(tái)的特性,讓異步操作更加的安全,方便和實(shí)用。實(shí)質(zhì)上它也是對(duì)java se庫(kù)中thread的一個(gè)封裝,加上了平臺(tái)相關(guān)的特性,所以對(duì)于所有的多線程異步都強(qiáng)烈推薦使用asynctask,因?yàn)樗紤],也融入了android平臺(tái)的特性,更加的安全和高效。
asynctask可以方便的執(zhí)行異步操作(doinbackground),又能方便的與主線程進(jìn)行通信,它本身又有良好的封裝性,可以進(jìn)行取消操作(cancel())。關(guān)于asynctask的使用,文檔說(shuō)的很明白,下面直接上實(shí)例。
實(shí)例
這個(gè)實(shí)例用asynctask到網(wǎng)絡(luò)上下載圖片,同時(shí)顯示進(jìn)度,下載完圖片更新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);
}
}
}
運(yùn)行結(jié)果
先后順序分別是下載前,下載中和下載后
總結(jié)
關(guān)于怎么使用看文檔和這個(gè)例子就夠了,下面說(shuō)下,使用時(shí)的注意事項(xiàng):
1. asynctask對(duì)象不可重復(fù)使用,也就是說(shuō)一個(gè)asynctask對(duì)象只能execute()一次,否則會(huì)有異常拋出"java.lang.illegalstateexception: cannot execute task: the task is already running"
2. 在doinbackground()中要檢查iscancelled()的返回值,如果你的異步任務(wù)是可以取消的話。
cancel()僅僅是給asynctask對(duì)象設(shè)置了一個(gè)標(biāo)識(shí)位,當(dāng)調(diào)用了cancel()后,發(fā)生的事情只有:asynctask對(duì)象的標(biāo)識(shí)位變了,和doinbackground()執(zhí)行完成后,onpostexecute()不會(huì)被回調(diào)了,而doinbackground()和onprogressupdate()還是會(huì)繼續(xù)執(zhí)行直到doinbackground()結(jié)束。所以要在doinbackground()中不斷的檢查iscancellled()的返回值,當(dāng)其返回true時(shí)就停止執(zhí)行,特別是有循環(huán)的時(shí)候。如上面的例子,如果把讀取數(shù)據(jù)的iscancelled()檢查去掉,圖片還是會(huì)下載,進(jìn)度也一直會(huì)走,只是最后圖片不會(huì)放到ui上(因?yàn)閛npostexecute()沒(méi)被回調(diào))!
這里的原因其實(shí)很好理解,想想java se的thread吧,是沒(méi)有方法將其直接cacncel掉的,那些線程取消也無(wú)非就是給線程設(shè)置標(biāo)識(shí)位,然后在run()方法中不斷的檢查標(biāo)識(shí)而已。
3. 如果要在應(yīng)用程序中使用網(wǎng)絡(luò),一定不要忘記在androidmanifest中聲明internet權(quán)限,否則會(huì)報(bào)出很詭異的異常信息,比如上面的例子,如果把internet權(quán)限拿掉會(huì)拋出"unknownhostexception"。剛開(kāi)始很疑惑,因?yàn)槟M器是可以正常上網(wǎng)的,后來(lái)google了下才發(fā)現(xiàn)原來(lái)是沒(méi)權(quán)限,但是疑問(wèn)還是沒(méi)有消除,既然沒(méi)有聲明網(wǎng)絡(luò)權(quán)限,為什么不直接提示無(wú)網(wǎng)絡(luò)權(quán)限呢?
對(duì)比java se的thread
thread是非常原始的類(lèi),它只有一個(gè)run()方法,一旦開(kāi)始,無(wú)法停止,它僅適合于一個(gè)非常獨(dú)立的異步任務(wù),也即不需要與主線程交互,對(duì)于其他情況,比如需要取消或與主線程交互,都需添加額外的代碼來(lái)實(shí)現(xiàn),并且還要注意同步的問(wèn)題。
而asynctask是封裝好了的,可以直接拿來(lái)用,如果你僅執(zhí)行獨(dú)立的異步任務(wù),可以僅實(shí)現(xiàn)doinbackground()。
所以,當(dāng)有一個(gè)非常獨(dú)立的任務(wù)時(shí),可以考慮使用thread,其他時(shí)候,盡可能的用asynctask。