Chinaunix首页 | 论坛 | 博客
  • 博客访问: 255431
  • 博文数量: 164
  • 博客积分: 60
  • 博客等级: 民兵
  • 技术积分: 1129
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-09 21:55
文章分类

全部博文(164)

文章存档

2017年(2)

2015年(67)

2014年(95)

我的朋友

分类: Android平台

2015-04-06 21:28:11



点击(此处)折叠或打开

  1. new AsyncTask<String, String, String>() {
  2.             @Override
  3.             protected void onPreExecute() {
  4.                 //线程执行前对应调用方法
  5.                 super.onPreExecute();
  6.             }
  7.             
  8.             @Override
  9.             protected String doInBackground(String... params) {
  10.                 //线程执行方法
  11.                 return null;
  12.             }
  13.             
  14.             @Override
  15.             protected void onPostExecute(String result) {
  16.                 //线程执行获取结果后调用方法
  17.                 super.onPostExecute(result);
  18.             }
  19.         }.execute("参数1","参数二");

  20.     public final AsyncTask<Params, Progress, Result> execute(Params... params) {
  21.           ....
  22.         //在线程开启前所做的预备操作
  23.         onPreExecute();

  24.         //mWorker?
  25.         //sExecutor?
  26.         //mFuture?

  27.         mWorker.mParams = params;
  28.         //开启线程的操作(sExecutor管理线程的类)
  29.         sExecutor.execute(mFuture);

  30.         return this;
  31.     }


  32.     //AsyncTask构造方法
  33.     
  34.     public AsyncTask() {
  35.         mWorker = new WorkerRunnable<Params, Result>() {
  36.             public Result call() throws Exception {
  37.                 ....
  38.                 //开启线程去请求网络
  39.                 return doInBackground(mParams);
  40.             }
  41.         };
  42.         
  43.         //FutureTask继承制Runnabel,run方法必须重写
  44.         mFuture = new FutureTask<Result>(mWorker) {
  45.             @Override
  46.             protected void done() {
  47.                 Message message;
  48.                 Result result = null;
  49.                 ...
  50.                 //其实获取到的就是doInBackground的一个返回值result
  51.                 result = get();
  52.                   ...
  53.                 //发送消息,
  54.                 //第一个就是当前消息的一个状态码,AsyncTaskResult封装了doInBackground返回值的对象

  55.                 message = sHandler.obtainMessage(MESSAGE_POST_RESULT,
  56.                         new AsyncTaskResult<Result>(AsyncTask.this, result));
  57.                 message.sendToTarget();
  58.             }
  59.         };
  60.     }
  61.     
  62.     //WorkerRunnable实现Callable接口,mWorker是Callable的子类对象
  63.     private static abstract class WorkerRunnable<Params, Result> implements Callable<Result> {
  64.         Params[] mParams;
  65.     }

  66.     //FutureTask接受Callable子类对象的构造方法
  67.     public FutureTask(Callable<V> callable) {
  68.         ...
  69.         sync = new Sync(callable);
  70.     }
  71.     
  72.     //将mWorker作为参数传递给当前Sync,全局变量
  73.     Sync(Callable<V> callable) {
  74.         this.callable = callable;
  75.     }

  76.     //FutureTask继承制Runnabel,run方法必须重写
  77.     public void run() {
  78.         sync.innerRun();
  79.     }

  80.     //innerRun对应实现

  81.     void innerRun() {
  82.            ...
  83.         //调用mWorker对应的call方法,在call方法中实现了调用doInBackground();
  84.         //开启线程会有一个返回值
  85.         result = callable.call();
  86.         ...
  87.         //将当前的result作为一个值,进行存储
  88.         set(result);
  89.         ...
  90.     }

  91.     //set方法
  92.     protected void set(V v) {
  93.         //转调innerset方法
  94.         sync.innerSet(v);
  95.     }

  96.     //
  97.     void innerSet(V v) {
  98.         ....
  99.         //当前done方法即是调用,AsyncTask构造方法中,创建FutureTask对象的时候,要去实现的方法
  100.         done();
  101.         ...
  102.     }
  103.     
  104.     //第一个参数就是AsyncTask对应对象,第二个参数即为doInBackground返回结果
  105.     private static class AsyncTaskResult<Data> {
  106.         final AsyncTask mTask;
  107.         final Data[] mData;

  108.         AsyncTaskResult(AsyncTask task, Data... data) {
  109.             mTask = task;
  110.             mData = data;
  111.         }
  112.     }
  113.     
  114.     private static class InternalHandler extends Handler {
  115.         @Override
  116.         public void handleMessage(Message msg) {
  117.             AsyncTaskResult result = (AsyncTaskResult) msg.obj;
  118.             switch (msg.what) {
  119.                 case MESSAGE_POST_RESULT:
  120.                     // There is only one result
  121.                     result.mTask.finish(result.mData[0]);
  122.                     break;
  123.             }
  124.         }
  125.     }

  126.     //将doInBackground返回结果作为参数传递进来
  127.     private void finish(Result result) {
  128.         ...
  129.         //请求网络完成后,填充视图
  130.         onPostExecute(result);
  131.         mStatus = Status.FINISHED;
  132.     }

阅读(1116) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~