[android 多线程]一些帖子与文章ProgressDialog, AsyncTask, Thread

本文介绍了Android中AsyncTask类的使用方法及注意事项,包括如何创建及执行异步任务、参数传递规则等,并提供了相关资源链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AsyncTask实现异步处理任务:http://www.iteye.com/topic/827513

我们都知道Android提供了一个较线程更简单的处理多任务的方法AsyncTask异步任务类,相对于线程来说AsyncTask对于简单的任务处理更安全,其内部的实现方法使用了AndroidHandler机制,对于常见的文件下载可以使用AsyncTask类来处理,在android系统中的Browser浏览器中就是用了该类下载Web服务器URLFavicon图标。见如下链接:

Android AsyncTask的异步任务

关于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的状态(大部分时间应该是即时的).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值