深入AsyncTask
2016-09-06 16:21
260 查看
AsyncTask<Params, Progress, Result>
一、Params:指定输入参数的类型
例如:指定输入参数的类型为String
AsyncTask<String, Void, Void>-->
task().execute("First parameter","Second parameter","Etc.")-->
doInBackground(String... params)
二、Progress:指定发送进度更新需要的类型
例如:指定发送进度更新需要的类型为Integer
在后台进程中调用publishProgress,则onProgressUpdate在主线程被调用。
三、Result 指定AsyncTask返回结果类型
例如:指定AsyncTask返回结果类型为ArrayList<GalleryItem>
private class FetchItemsTask extends AsyncTask<Void,Void,ArrayList<GalleryItem>>{
@Override
protected ArrayList<GalleryItem> doInBackground(Void... params) {
return new FlickrFetchr().fetchItems();
}
@Override
protected void onPostExecute(ArrayList<GalleryItem> galleryItems) {
mItems=galleryItems;
setupAdapter();
}
}
doInBackground把返回结果传递给了onPostExecute
四、清理AsyncTask
AsyncTask task=new
FetchItemsTask();
task.cancel(false):提前结束运行。优先考虑
task.cancel(true):终止doInBackground所在的线程。
一、Params:指定输入参数的类型
例如:指定输入参数的类型为String
AsyncTask<String, Void, Void>-->
task().execute("First parameter","Second parameter","Etc.")-->
doInBackground(String... params)
二、Progress:指定发送进度更新需要的类型
例如:指定发送进度更新需要的类型为Integer
在后台进程中调用publishProgress,则onProgressUpdate在主线程被调用。
三、Result 指定AsyncTask返回结果类型
例如:指定AsyncTask返回结果类型为ArrayList<GalleryItem>
private class FetchItemsTask extends AsyncTask<Void,Void,ArrayList<GalleryItem>>{
@Override
protected ArrayList<GalleryItem> doInBackground(Void... params) {
return new FlickrFetchr().fetchItems();
}
@Override
protected void onPostExecute(ArrayList<GalleryItem> galleryItems) {
mItems=galleryItems;
setupAdapter();
}
}
doInBackground把返回结果传递给了onPostExecute
四、清理AsyncTask
AsyncTask task=new
FetchItemsTask();
task.cancel(false):提前结束运行。优先考虑
task.cancel(true):终止doInBackground所在的线程。
相关文章推荐
- mac版eclipse 转到windows eclipse后问题
- Learnning Dlib(六) Speeding up Dlib’s Facial Landmark Detector
- caffe中网络结构参数详解
- Java中字符串常量相加
- JAVA基础面试及笔试题
- Nginx初探之二
- 触发器同步
- 支付宝pc端支付接入PHP实现
- swift-sharesdk集成微信、Facebook第三方登录
- 使用栈解决迷宫问题
- libvirt依赖的库
- Spring BeanUtils.copyProperties和apache commons-beanutils
- BroadcastReceiver学习之路(01)之普通广播及广播简介
- 使用北通手柄控制turtlesim运动
- 【递归、二分查找】数字在排序数组中出现的次数
- python gui 之 tkinter库
- Linux kernel Low Level debug
- tomcat7+jdk的keytool生成证书 配置https
- Java程序员的10个日志技巧
- ios上取得设备唯一标志的解决方案