Android开发笔记之:深入理解多线程AsyncTask
程序员文章站
2023-11-23 22:30:46
understanding asynctaskasynctask是android 1.5 cubake加入的用于实现异步操作的一个类,在此之前只能用java se库中的th...
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。
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。
上一篇: 基于Android中dp和px之间进行转换的实现代码
下一篇: SAI如何调色?SAI调色教程