AsyncTask的简单用法

 

AsyncTask 是Android提供的一个简单工具类,适用于简单的异步处理,它使创建需要与用户界面交互的长时间运行的任务变得更简单,不需要借助线程和Handler即可实现。

下面就来看看AsyncTask的简单用法:

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called ParamsProgress and Result, and 4 steps, called onPreExecutedoInBackgroundonProgressUpdate and onPostExecute.

AsyncTask 定义了一个运行在后台线程中的计算任务,计算的结果将会在UI线程中显示出来。一个AsyncTask有三个泛型参数:

Params:传入的参数

Progress:计算过程,可以用于显示在UI上

Result:后台计算的结果,可以用于UI线程显示最终结果。

和四个步骤:

 

  1. onPreExecute(), invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.
  2. doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.
  3. onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.
  4. onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

 

onPreExecute:在 task 被执行前,由UI线程调用

doInBackground:onPreExecute()结束之后调用该方法,通常用于计算后台的耗时任务。计算过程中,可以使用publishProgress(Progress...)方法来更新UI线程界面,它的计算结果会传递到onPostExecute(Result)中。

onProgressUpdate:publishProgress(Progress...)方法被调用之后,UI线程会调用该方法来刷新界面

onPostExecute:后台任务完成之后,UI线程会调用该方法

 

应用中,我们通常覆盖上面的方法,其中doInBackground()是必须的。

 

一个简单的例子(后台计算Fibonacci数列):

package com.win.asynctasktest;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class FibonacyAsync extends AsyncTask<Integer, Double, Long> {

private static final int FIRST = 1;
private static final int SECOND = 2;

private static long ret;
private Context mContext;

public FibonacyAsync(Context c) {
mContext = c;
}

@Override
protected Long doInBackground(Integer... params) {
// TODO Auto-generated method stub
int n = params[0].intValue();
ret = fibAsync(n);

return (long) ret;
}

@Override
protected void onCancelled() {
// TODO Auto-generated method stub
super.onCancelled();
}

@Override
protected void onPostExecute(Long result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}

@Override
protected void onProgressUpdate(Double... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}


public static int fibAsync(int n) {
if (sFirstCome) {
sMax = n;
sFirstCome = false;
}

per = n / (double) sMax;

if (n > 2) {
return fibAsync(n - 1) + fibAsync(n - 2);
} else if (n == FIRST || n == SECOND) {
return 1;
} else {
System.out.println("We are located wrongly, what's happended!");
return -1;
}
}
}

 

posted @ 2013-03-23 12:51  hunterDing  阅读(155)  评论(0编辑  收藏  举报