我不理解我应该在这里放什么以及这些参数将在哪里结束?我应该放什么,它会放在哪里?我需要包含全部三个还是可以只包含1、2、20?
我不理解我应该在这里放什么以及这些参数将在哪里结束?我应该放什么,它会放在哪里?我需要包含全部三个还是可以只包含1、2、20?
谷歌的Android文档说:
异步任务由3个泛型类型(称为Params,Progress和Result)和4个步骤(称为onPreExecute,doInBackground,onProgressUpdate和onPostExecute)定义。
AsyncTask的泛型类型:
异步任务使用的三种类型如下:
Params, the type of the parameters sent to the task upon execution.
Progress, the type of the progress units published during the background computation.
Result, the type of the result of the background computation.
并不是所有的类型都会被异步任务使用。如果要标记一个类型为未使用,只需使用类型 Void:
private class MyTask extends AsyncTask<Void, Void, Void> { ... }
您可以进一步参考:http://developer.android.com/reference/android/os/AsyncTask.html
或者您可以通过参考Sankar-Ganesh的博客来清楚AsyncTask的作用。
private class MyTask extends AsyncTask<X, Y, Z>
protected void onPreExecute(){
}
这个方法会在启动新线程之前执行。由于没有输入/输出值,所以你可以初始化变量或者进行其他你认为需要做的操作。
protected Z doInBackground(X...x){
}
AsyncTask类中最重要的方法。您必须在此处放置所有希望在后台执行的任务,使用与主线程不同的线程。这里我们有一个输入值,它是来自类型“X”的对象数组(你看到标题中的吗?我们有“...extends AsyncTask”这些是输入参数的类型),并返回一个类型为“Z”的对象。
protected void onProgressUpdate(Y y){
}
使用方法 publishProgress(y) 调用此方法,通常在您希望在主屏幕上显示任何进度或信息时使用,例如进度条显示您在后台执行的操作的进度。
protected void onPostExecute(Z z){
}
这个方法在后台操作完成后被调用。作为输入参数,您将收到doInBackground方法的输出参数。
X、Y和Z类型怎么办?
从上面的结构可以推断出:
X – The type of the input variables value you want to set to the background process. This can be an array of objects.
Y – The type of the objects you are going to enter in the onProgressUpdate method.
Z – The type of the result from the operations you have done in the background process.
我们如何从外部类调用此任务?只需使用以下两行代码:
MyTask myTask = new MyTask();
myTask.execute(x);
其中,x是类型X的输入参数。
一旦我们的任务开始运行,我们可以从“外部”获取其状态。使用“getStatus()”方法。
myTask.getStatus();
我们可以接收到以下状态:
RUNNING - 表示任务正在运行。
PENDING - 表示任务尚未被执行。
FINISHED - 表示onPostExecute(Z)已经完成。
使用AsyncTask的提示
不要手动调用onPreExecute,doInBackground和onPostExecute方法。系统会自动处理。
您不能在另一个AsyncTask或线程中调用AsyncTask。必须在UI线程中调用execute方法。
onPostExecute方法在UI线程中执行(在这里可以调用另一个AsyncTask!)。
任务的输入参数可以是对象数组,这样您就可以放置任何对象和类型。
AsyncTask
是在后台线程中运行的后台任务,它接受一个输入,执行进度并给出输出。
AsyncTask中的参数。 关键是不要死记硬背。 如果您能够想象出您的任务实际需要做什么,那么编写具有正确签名的即
AsyncTask<Input,Progress,Output>
。
AsyncTask
将非常容易。 只需找出您的输入,进度和输出即可。doInBackground()
方法是AsyncTask
中最重要的方法,因为
AsyncTask
参数而改变。因此,让我们看看它们之间的关系
doInBackground()
和onPostExecute()
、onProgressUpdate()
也是相关的
给我看代码
那么我该如何编写DownloadTask的代码?
DownloadTask extends AsyncTask<String,Integer,String>{
@Override
public void onPreExecute()
{}
@Override
public String doInbackGround(String... params)
{
// Download code
int downloadPerc = // calculate that
publish(downloadPerc);
return "Download Success";
}
@Override
public void onPostExecute(String result)
{
super.onPostExecute(result);
}
@Override
public void onProgressUpdate(Integer... params)
{
// show in spinner, access UI elements
}
}
你将如何运行此任务
new DownLoadTask().execute("Paradise.mp3");
onProgressUpdate(...)
最直接的解释 :) - knoxgon1. private class DownloadFilesTask extends AsyncTask<URL, Integer, Long>
2. protected class InitTask extends AsyncTask<Context, Integer, Integer>
异步任务是指在后台线程上运行的计算,其结果在UI线程上发布。异步任务由3个泛型类型Params、Progress和Result定义,并且有4个步骤,称为onPreExecute、doInBackground、onProgressUpdate和onPostExecute。
简而言之,AsyncTask中有3个参数:
用于在DoInBackground(String... params)中使用的输入参数
用于在OnProgressUpdate(String... status)中显示进度状态的参数
用于在OnPostExecute(String... result)中使用的结果参数
注意:参数类型可能因您的需求而异。