昊仔

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

本博文地址:http://blog.csdn.net/mylzc/article/details/6774131,转载请注明出处

Android异步处理系列文章索引

Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面

Android异步处理二:使用AsyncTask异步更新UI界面

Android异步处理三:Handler+Looper+MessageQueue深入详解

Android异步处理四:AsyncTask的实现原理

 

《Android异步处理二:使用AsyncTask异步更新UI界面》一文中,我们介绍了如何使用AsyncTask实现异步下载图片,并且更新图片到UI界面的方法。本篇我们将学习Framework层AsyncTask的实现原理。

概述:AsyncTask的本质是一个线程池,所有提交的异步任务都会在这个线程池中的工作线程内执行,当工作线程需要跟UI线程交互时,工作线程会通过向在UI线程创建的Handler(原理见:《Android异步处理三:Handler+Looper+MessageQueue深入详解》传递消息的方式,调用相关的回调函数,从而实现UI界面的更新。

 

例子:

本章还是以《Android异步处理二:使用AsyncTask异步更新UI界面》中的例子说明AsyncTask的实现原理。

这个例子是在后台下载CSDN的LOGO,下载完成后在UI界面上显示出来。

 

AsyncTask.java

 

  1. package com.zhuozhuo; 
  2.  
  3.  
  4. import org.apache.http.HttpResponse; 
  5. import org.apache.http.client.HttpClient; 
  6. import org.apache.http.client.methods.HttpGet; 
  7. import org.apache.http.impl.client.DefaultHttpClient; 
  8.  
  9. import android.app.Activity; 
  10. import android.graphics.Bitmap; 
  11. import android.graphics.BitmapFactory; 
  12. import android.os.AsyncTask; 
  13. import android.os.Bundle; 
  14. import android.view.View; 
  15. import android.view.View.OnClickListener; 
  16. import android.widget.Button; 
  17. import android.widget.ImageView; 
  18. import android.widget.ProgressBar; 
  19. import android.widget.Toast; 
  20.  
  21. publicclass AsyncTaskActivity extends Activity { 
  22.      
  23.     private ImageView mImageView; 
  24.     private Button mButton; 
  25.     private ProgressBar mProgressBar; 
  26.      
  27.     @Override 
  28.     publicvoid onCreate(Bundle savedInstanceState) { 
  29.         super.onCreate(savedInstanceState); 
  30.         setContentView(R.layout.main); 
  31.          
  32.         mImageView= (ImageView) findViewById(R.id.imageView); 
  33.         mButton = (Button) findViewById(R.id.button); 
  34.         mProgressBar = (ProgressBar) findViewById(R.id.progressBar); 
  35.         mButton.setOnClickListener(new OnClickListener() { 
  36.              
  37.             @Override 
  38.             publicvoid onClick(View v) { 
  39.                 GetCSDNLogoTask task = new GetCSDNLogoTask(); 
  40.                 task.execute("http://csdnimg.cn/www/images/csdnindex_logo.gif"); 
  41.             } 
  42.         }); 
  43.     } 
  44.      
  45.     class GetCSDNLogoTask extends AsyncTask<String,Integer,Bitmap> {//继承AsyncTask  
  46.  
  47.         @Override 
  48.         protected Bitmap doInBackground(String... params) {//处理后台执行的任务,在后台线程执行  
  49.             publishProgress(0);//将会调用onProgressUpdate(Integer... progress)方法  
  50.             HttpClient hc = new DefaultHttpClient(); 
  51.             publishProgress(30); 
  52.             HttpGet hg = new HttpGet(params[0]);//获取csdn的logo  
  53.             final Bitmap bm; 
  54.             try
  55.                 HttpResponse hr = hc.execute(hg); 
  56.                 bm = BitmapFactory.decodeStream(hr.getEntity().getContent()); 
  57.             } catch (Exception e) { 
  58.                  
  59.                 returnnull
  60.             } 
  61.             publishProgress(100); 
  62.             //mImageView.setImageBitmap(result); 不能在后台线程操作ui  
  63.             return bm; 
  64.         } 
  65.          
  66.         protectedvoid onProgressUpdate(Integer... progress) {//在调用publishProgress之后被调用,在ui线程执行  
  67.             mProgressBar.setProgress(progress[0]);//更新进度条的进度  
  68.          } 
  69.  
  70.          protectedvoid onPostExecute(Bitmap result) {//后台任务执行完之后被调用,在ui线程执行  
  71.              if(result != null) { 
  72.                  Toast.makeText(AsyncTaskActivity.this, "成功获取图片", Toast.LENGTH_LONG).show(); 
  73.                  mImageView.setImageBitmap(result); 
  74.              }else
  75.                  Toast.makeText(AsyncTaskActivity.this, "获取图片失败", Toast.LENGTH_LONG).show(); 
  76.              } 
  77.          } 
  78.           
  79.          protectedvoid onPreExecute () {//在 doInBackground(Params...)之前被调用,在ui线程执行  
  80.              mImageView.setImageBitmap(null); 
  81.              mProgressBar.setProgress(0);//进度条复位  
  82.          } 
  83.           
  84.          protectedvoid onCancelled () {//在ui线程执行  
  85.              mProgressBar.setProgress(0);//进度条复位  
  86.          } 
  87.          
  88.     } 
  89.      
  90.  
package com.zhuozhuo;


import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class AsyncTaskActivity extends Activity {
    
	private ImageView mImageView;
	private Button mButton;
	private ProgressBar mProgressBar;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mImageView= (ImageView) findViewById(R.id.imageView);
        mButton = (Button) findViewById(R.id.button);
        mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
        mButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				GetCSDNLogoTask task = new GetCSDNLogoTask();
				task.execute("http://csdnimg.cn/www/images/csdnindex_logo.gif");
			}
		});
    }
    
    class GetCSDNLogoTask extends AsyncTask<String,Integer,Bitmap> {//继承AsyncTask

		@Override
		protected Bitmap doInBackground(String... params) {//处理后台执行的任务,在后台线程执行
			publishProgress(0);//将会调用onProgressUpdate(Integer... progress)方法
			HttpClient hc = new DefaultHttpClient();
			publishProgress(30);
			HttpGet hg = new HttpGet(params[0]);//获取csdn的logo
			final Bitmap bm;
			try {
				HttpResponse hr = hc.execute(hg);
				bm = BitmapFactory.decodeStream(hr.getEntity().getContent());
			} catch (Exception e) {
				
				return null;
			}
			publishProgress(100);
			//mImageView.setImageBitmap(result); 不能在后台线程操作ui
			return bm;
		}
		
		protected void onProgressUpdate(Integer... progress) {//在调用publishProgress之后被调用,在ui线程执行
			mProgressBar.setProgress(progress[0]);//更新进度条的进度
	     }

	     protected void onPostExecute(Bitmap result) {//后台任务执行完之后被调用,在ui线程执行
	    	 if(result != null) {
	    		 Toast.makeText(AsyncTaskActivity.this, "成功获取图片", Toast.LENGTH_LONG).show();
	    		 mImageView.setImageBitmap(result);
	    	 }else {
	    		 Toast.makeText(AsyncTaskActivity.this, "获取图片失败", Toast.LENGTH_LONG).show();
	    	 }
	     }
	     
	     protected void onPreExecute () {//在 doInBackground(Params...)之前被调用,在ui线程执行
	    	 mImageView.setImageBitmap(null);
	    	 mProgressBar.setProgress(0);//进度条复位
	     }
	     
	     protected void onCancelled () {//在ui线程执行
	    	 mProgressBar.setProgress(0);//进度条复位
	     }
    	
    }
    

}

main.xml

 

 

  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical"android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent"> 
  5.     <ProgressBarandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:id="@+id/progressBar"style="?android:attr/progressBarStyleHorizontal"></ProgressBar> 
  6.     <Buttonandroid:id="@+id/button"android:text="下载图片"android:layout_width="wrap_content"android:layout_height="wrap_content"></Button> 
  7.     <ImageViewandroid:id="@+id/imageView"android:layout_height="wrap_content" 
  8.         android:layout_width="wrap_content"/> 
  9. </LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
    <ProgressBar android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/progressBar" style="?android:attr/progressBarStyleHorizontal"></ProgressBar>
    <Button android:id="@+id/button" android:text="下载图片" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
	<ImageView android:id="@+id/imageView" android:layout_height="wrap_content"
		android:layout_width="wrap_content" />
</LinearLayout>

mainifest.xml

  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android" 
  3.       package="com.zhuozhuo" 
  4.       android:versionCode="1" 
  5.       android:versionName="1.0"> 
  6.     <uses-sdkandroid:minSdkVersion="10"/> 
  7. <uses-permissionandroid:name="android.permission.INTERNET"></uses-permission> 
  8.     <applicationandroid:icon="@drawable/icon"android:label="@string/app_name"> 
  9.         <activityandroid:name=".AsyncTaskActivity" 
  10.                   android:label="@string/app_name"> 
  11.             <intent-filter> 
  12.                 <actionandroid:name="android.intent.action.MAIN"/> 
  13.                 <categoryandroid:name="android.intent.category.LAUNCHER"/> 
  14.             </intent-filter> 
  15.         </activity> 
  16.  
  17.     </application> 
  18. </manifest> 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.zhuozhuo"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AsyncTaskActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

运行结果:

 

分析:

在分析实现流程之前,我们先了解一下AsyncTask有哪些成员变量。

 

  1. privatestaticfinalint CORE_POOL_SIZE =5;//5个核心工作线程  
  2. privatestaticfinalint MAXIMUM_POOL_SIZE = 128;//最多128个工作线程  
  3. privatestaticfinalint KEEP_ALIVE = 1;//空闲线程的超时时间为1秒  
  4.  
  5. privatestaticfinal BlockingQueue<Runnable> sWorkQueue = 
  6.         new LinkedBlockingQueue<Runnable>(10);//等待队列  
  7.  
  8. privatestaticfinal ThreadPoolExecutorsExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE, 
  9.         MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, sWorkQueue,sThreadFactory);//线程池是静态变量,所有的异步任务都会放到这个线程池的工作线程内执行。 
   private static final int CORE_POOL_SIZE =5;//5个核心工作线程
   private static final int MAXIMUM_POOL_SIZE = 128;//最多128个工作线程
   private static final int KEEP_ALIVE = 1;//空闲线程的超时时间为1秒
 
   private static final BlockingQueue<Runnable> sWorkQueue =
           new LinkedBlockingQueue<Runnable>(10);//等待队列
 
   private static final ThreadPoolExecutorsExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE,
           MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, sWorkQueue,sThreadFactory);//线程池是静态变量,所有的异步任务都会放到这个线程池的工作线程内执行。

 

 

回到例子中,点击按钮之后会新建一个GetCSDNLogoTask对象:

 

  1. GetCSDNLogoTask task = new GetCSDNLogoTask(); 
GetCSDNLogoTask task = new GetCSDNLogoTask();

 

此时会调用父类AsyncTask的构造函数:

AsyncTask.java

 

  1. public AsyncTask() { 
  2.         mWorker = new WorkerRunnable<Params, Result>() { 
  3.             public Result call() throws Exception { 
  4.                 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); 
  5.                 return doInBackground(mParams); 
  6.             } 
  7.         }; 
  8.  
  9.         mFuture = new FutureTask<Result>(mWorker) { 
  10.             @Override 
  11.             protectedvoid done() { 
  12.                 Message message; 
  13.                 Result result = null
  14.  
  15.                 try
  16.                     result = get(); 
  17.                 } catch (InterruptedException e) { 
  18.                     android.util.Log.w(LOG_TAG, e); 
  19.                 } catch (ExecutionException e) { 
  20.                     thrownew RuntimeException("An error occured while executing doInBackground()"
  21.                             e.getCause()); 
  22.                 } catch (CancellationException e) { 
  23.                     message = sHandler.obtainMessage(MESSAGE_POST_CANCEL, 
  24.                             new AsyncTaskResult<Result>(AsyncTask.this, (Result[]) null)); 
  25.                     message.sendToTarget();//取消任务,发送MESSAGE_POST_CANCEL消息  
  26.                     return
  27.                 } catch (Throwable t) { 
  28.                     thrownew RuntimeException("An error occured while executing " 
  29.                             + "doInBackground()", t); 
  30.                 } 
  31.  
  32.                 message = sHandler.obtainMessage(MESSAGE_POST_RESULT, 
  33.                         new AsyncTaskResult<Result>(AsyncTask.this, result));//完成任务,发送MESSAGE_POST_RESULT消息并传递result对象  
  34.                 message.sendToTarget(); 
  35.             } 
  36.         }; 
  37.     } 
public AsyncTask() {
        mWorker = new WorkerRunnable<Params, Result>() {
            public Result call() throws Exception {
                Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
                return doInBackground(mParams);
            }
        };

        mFuture = new FutureTask<Result>(mWorker) {
            @Override
            protected void done() {
                Message message;
                Result result = null;

                try {
                    result = get();
                } catch (InterruptedException e) {
                    android.util.Log.w(LOG_TAG, e);
                } catch (ExecutionException e) {
                    throw new RuntimeException("An error occured while executing doInBackground()",
                            e.getCause());
                } catch (CancellationException e) {
                    message = sHandler.obtainMessage(MESSAGE_POST_CANCEL,
                            new AsyncTaskResult<Result>(AsyncTask.this, (Result[]) null));
                    message.sendToTarget();//取消任务,发送MESSAGE_POST_CANCEL消息
                    return;
                } catch (Throwable t) {
                    throw new RuntimeException("An error occured while executing "
                            + "doInBackground()", t);
                }

                message = sHandler.obtainMessage(MESSAGE_POST_RESULT,
                        new AsyncTaskResult<Result>(AsyncTask.this, result));//完成任务,发送MESSAGE_POST_RESULT消息并传递result对象
                message.sendToTarget();
            }
        };
    }

 

WorkerRunnable类实现了callable接口的call()方法,该函数会调用我们在AsyncTask子类中实现的doInBackground(mParams)方法,由此可见,WorkerRunnable封装了我们要执行的异步任务。FutureTask中的protected void done() {}方法实现了异步任务状态改变后的操作。当异步任务被取消,会向UI线程传递MESSAGE_POST_CANCEL消息,当任务成功执行,会向UI线程传递MESSAGE_POST_RESULT消息,并把执行结果传递到UI线程。

由此可知,AsyncTask在构造的时候已经定义好要异步执行的方法doInBackground(mParams)和任务状态变化后的操作(包括失败和成功)。

当创建完GetCSDNLogoTask对象后,执行

 

  1. task.execute("http://csdnimg.cn/www/images/csdnindex_logo.gif"); 
task.execute("http://csdnimg.cn/www/images/csdnindex_logo.gif");

 

此时会调用AsyncTask的execute(Params...params)方法

 

AsyncTask.java

 

  1. publicfinal AsyncTask<Params,Progress, Result> execute(Params... params) { 
  2.         if (mStatus != Status.PENDING) { 
  3.             switch (mStatus) { 
  4.                 case RUNNING: 
  5.                     throw newIllegalStateException("Cannot execute task:" 
  6.                             + " the taskis already running."); 
  7.                 case FINISHED: 
  8.                     throw newIllegalStateException("Cannot execute task:" 
  9.                             + " the taskhas already been executed " 
  10.                             + "(a task canbe executed only once)"); 
  11.             } 
  12.         } 
  13.   
  14.         mStatus = Status.RUNNING; 
  15.   
  16.         onPreExecute();//运行在ui线程,在提交任务到线程池之前执行  
  17.   
  18.         mWorker.mParams = params; 
  19.         sExecutor.execute(mFuture);//提交任务到线程池  
  20.   
  21.         returnthis
public final AsyncTask<Params,Progress, Result> execute(Params... params) {
        if (mStatus != Status.PENDING) {
            switch (mStatus) {
                case RUNNING:
                    throw newIllegalStateException("Cannot execute task:"
                            + " the taskis already running.");
                case FINISHED:
                    throw newIllegalStateException("Cannot execute task:"
                            + " the taskhas already been executed "
                            + "(a task canbe executed only once)");
            }
        }
 
        mStatus = Status.RUNNING;
 
        onPreExecute();//运行在ui线程,在提交任务到线程池之前执行
 
        mWorker.mParams = params;
        sExecutor.execute(mFuture);//提交任务到线程池
 
        return this;
}

 

当任务正在执行或者已经完成,会抛出IllegalStateException,由此可知我们不能够重复调用execute(Params...params)方法。在提交任务到线程池之前,调用了onPreExecute()方法。然后才执行sExecutor.execute(mFuture)是任务提交到线程池。

前面我们说到,当任务的状态发生改变时(1、执行成功2、取消执行3、进度更新),工作线程会向UI线程的Handler传递消息。在《Android异步处理三:Handler+Looper+MessageQueue深入详解》一文中我们提到,Handler要处理其他线程传递过来的消息。在AsyncTask中,InternalHandler是在UI线程上创建的,它接收来自工作线程的消息,实现代码如下:

 

AsyncTask.java

 

  1. privatestaticclass InternalHandler extends Handler { 
  2.        @SuppressWarnings({"unchecked","RawUseOfParameterizedType"}) 
  3.         @Override 
  4.         public voidhandleMessage(Message msg) { 
  5.             AsyncTaskResult result =(AsyncTaskResult) msg.obj; 
  6.             switch (msg.what) { 
  7.                 caseMESSAGE_POST_RESULT: 
  8.                     // There is onlyone result  
  9.                     result.mTask.finish(result.mData[0]);//执行任务成功  
  10.                     break
  11.                 caseMESSAGE_POST_PROGRESS: 
  12.                    result.mTask.onProgressUpdate(result.mData);//进度更新  
  13.                     break
  14.                 caseMESSAGE_POST_CANCEL: 
  15.                     result.mTask.onCancelled();//取消任务  
  16.                     break
  17.             } 
  18.         } 
  19.     } 
private static class InternalHandler extends Handler {
       @SuppressWarnings({"unchecked","RawUseOfParameterizedType"})
        @Override
        public voidhandleMessage(Message msg) {
            AsyncTaskResult result =(AsyncTaskResult) msg.obj;
            switch (msg.what) {
                caseMESSAGE_POST_RESULT:
                    // There is onlyone result
                    result.mTask.finish(result.mData[0]);//执行任务成功
                    break;
                caseMESSAGE_POST_PROGRESS:
                   result.mTask.onProgressUpdate(result.mData);//进度更新
                    break;
                caseMESSAGE_POST_CANCEL:
                    result.mTask.onCancelled();//取消任务
                    break;
            }
        }
    }

 

当接收到消息之后,AsyncTask会调用自身相应的回调方法。

总结:

1、 AsyncTask的本质是一个静态的线程池,AsyncTask派生出的子类可以实现不同的异步任务,这些任务都是提交到静态的线程池中执行。

2、线程池中的工作线程执行doInBackground(mParams)方法执行异步任务

3、当任务状态改变之后,工作线程会向UI线程发送消息,AsyncTask内部的InternalHandler响应这些消息,并调用相关的回调函数

 

本博文地址:http://blog.csdn.net/mylzc/article/details/6774131,转载请注明出处

posted on 2013-08-20 13:30  昊仔  阅读(270)  评论(0编辑  收藏  举报