Android异步AsyncTask二三解
execute()向doInBackground()传递。//第1个泛型参数
doInBackground()的返回值会传递给onPostExecute()。//第3个泛型参数
publishProgress()向progressUpdate()传递。//第2个泛型参数
在实例化异步的时候,只需要调用execute(Object... params)方法,就会自动调用doInBackground()并把参数传入。
class MyAsyncTask extends AsyncTask<String, TextView, Double>{
private TextView mTv;
public MyAsyncTask(TextView tv){
this.mTv=tv;
}
@Override
protected Double doInString... params) { < /span>
System.out.println("doInBackground()");
double dou=0;
if (params[0].equals("wei")) {
dou=100;
}
publishProgress(mTv);
return dou;
}
@Override
protectedvoid onPreExecute() {
System.out.println("onPreExecute()");
super.onPreExecute();
}
@Override
protectedvoid onPostExecute(Double result) {
System.out.println("onPostExecute()");
super.onPostExecute(result);
}
@Override
protectedvoid onProgressUpdate(TextView... values) {
System.out.println("onProgressUpdate()");
values[0].setText("bing");
super.onProgressUpdate(values);
}
}
调用
mMyAsyncTask=new MyAsyncTask(tv);
mMyAsyncTask.execute("wei");
更多相关文章
- Android(安卓)7.0及以上调用系统相机拍照、访问相册问题
- 在代码中实现按下Home键的效果
- Android(安卓)显示系统分析
- Tween动画介绍
- android编程如何让程序后台运行
- Android之监测database的改变--notifyChange
- 关于使用AccountManager的remove删除Android帐号的细节
- 2011.07.12(3)——— android ui的一些概念
- View绘制流程源码解析-第一篇