android-AsyncTask类

public abstract class

AsyncTask

extends  Object
java.lang.Object
  android.os.AsyncTask<Params, Progress, Result>

类概述

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

异步任务让我们更加的容易恰当的使用UI线程。这个类允许执行后台操作,并在不操作子线程或者handler的情况下,更新UI。

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, calledParamsProgress and Result, and 4 steps, called begindoInBackgroundprocessProgress and end.

一个异步任务是一个运行在后台现成的过程,它的结果可以更新到UI主线程。它有3个泛型参数,ParamsProgress and Result,需要4个步骤begindoInBackgroundprocessProgress and end.


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.

在task执行之前UI线程调用。通常用于设置task,例如,在UI中展示进度条。

oInBackground(), 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() to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate step.

onPreExecute() 结束后立即执行。这一步用于执行耗时的后台运算。异步任务的参数parameters 被传递到此函数。计算结果必须在此函数返回,并将被传到下一步函数。这个函数也可以调用publishProgress() 来发布进程的一些信息。这些值被发布到UI线程。


onProgressUpdate(), invoked on the UI thread after a call to publishProgress(). 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.

调用 publishProgress() 是被调用。执行的时间不确定。当后台的计算执行时,这函数用来展示UI界面上进度条的刻度。例如,他可以用来更新进度条或者在textfield显示日志。

onPostExecute(), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

当后台的计算结束后被UI线程调用。后台运算的结果作为参数被传送到这个函数。

Cancelling a task
 A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object), instead of
onPostExecute(Object) will be invoked after doInBackground(Object[]) returns. To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from
doInBackground(Object[]), if possible (inside a loop for instance.)

取消任务

通过cancel(boolean),任务可以在任何时刻被取消。调用这个方法将随后引起调用isCancelled()来返回true。但调用这个方法后,代替onPostExecute(Object) ,onCancelled(Object)将会被调用在doInBackground(Object[]) 返回之后。为了确保任务尽快的被取消,如果可能的话,你应该经常定期的在doInBackground(Object[])里检测isCancelled()。


class MyAsynTask extends AsyncTask<Void, Void, Void> {
public MyAsynTask() {
// TODO Auto-generated constructor stub
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
URL url = new URL(
"http://img0.bdstatic.com/img/image/shouye/sheying0424.jpg");
InputStream is = url.openStream();
bitmap = BitmapFactory.decodeStream(is);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
Log.e("----", "onPostExecute");
imageView.setImageBitmap(bitmap);
}
protected void onPreExecute(){
Log.e("----", "onPreExecute");
}
}

这是定义的异步任务,从网站上下载图片,设置到UI中。

需要注意的是

onPostExecute()里调用的函数imageView.setImageBitmap(bitmap);千万不能doInBackground()中调用,

会报错。不知道android怎么实现的,但是觉得很神奇。doInBackground()函数里做些下载,循环之类的就结束,

对结果的处理,放在onPostExecute()中。


另外,AsyncTask<Void, Void, Void> 这里面的Void,是void的占位符类,要传int 此处要写Integer 才行。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值