AsyncTask实现异步处理任务:http://www.iteye.com/topic/827513
我们都知道Android提供了一个较线程更简单的处理多任务的方法AsyncTask异步任务类,相对于线程来说AsyncTask对于简单的任务处理更安全,其内部的实现方法使用了Android的Handler机制,对于常见的文件下载可以使用AsyncTask类来处理,在android系统中的Browser浏览器中就是用了该类下载Web服务器URL的Favicon图标。见如下链接:
关于AsyncTask的几点注意事项(摘自androiddoc):
Threading rules There are a few threading rules that must be followed for this class to work properly: (1)The task instance must be created on the UI thread. (2)execute(Params...) must be invoked on the UI thread. (3)Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually. (4)The task can be executed only once (an exception will be thrown if a second execution is attempted.) 注意其中四个函数之间的参数传递,大有学问!具体可以参见文档
android多线程:http://www.360doc.com/content/10/1108/11/3779243_67585561.shtml
ProgressDialog 使用:http://www.iteye.com/topic/569152
Android下的多线程:http://justjavac.iteye.com/blog/699735
Android多线程:http://blog.youkuaiyun.com/anghlq/archive/2010/05/20/5612592.aspx
http://www.eoeandroid.com/thread-2089-1-1.html
需要注意的是:
在Android中非UI线程,是不能触碰UI类的。也就是说非主线程不能直接刷新主线程的界面,否则就会报
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.(可见:http://hi.baidu.com/lfcaolibin/blog/item/5cdf623c39ea760bbaa16738.html)
解决方式是另起一个Handler,将改变UI的代码封装到里面,子线程来调用这个handler,完成工作之后发送一个消息到消息队列中,主线程在空闲的时候来更新UI的状态(大部分时间应该是即时的).