class GetXMLTask extends AsyncTask<String,Integer,Bitmap> {//继承AsyncTask
@Override
protected Bitmap doInBackground(String... params) {
//处理后台执行的任务,在后台线程执行
return bmImg;
}
protected void onProgressUpdate(Integer... progress) {
//在调用publishProgress之后被调用,在ui线程执行
}
protected void onPostExecute(Bitmap result) {
//后台任务执行完之后被调用,在ui线程执行
if(result != null) {
}else {
}
mpDialog.cancel();
}
protected void onPreExecute () {
//在 doInBackground(Params...)之前被调用,在ui线程执行
mpDialog.setMessage("亲,正在帮您努力加载哦!");
mpDialog.show();
}
protected void onCancelled () {
//在ui线程执行
}
}
我们可以看到,主要就下面
onPreExecute //刷新ui可以提示用户开始后台工作了
doInBackground //后台执行,比如下载等等onPostExecute //后台执行后,刷新界面。
参考资料
本文详细介绍了Android中AsyncTask类的使用方法,包括其生命周期方法如onPreExecute、doInBackground及onPostExecute的作用与应用场景,帮助开发者更好地理解并利用AsyncTask进行异步任务处理。
7万+

被折叠的 条评论
为什么被折叠?



