mer*_*ort 153 android arguments android-asynctask
我不明白我应该放在这里以及这些论点最终的位置?究竟应该放什么,它究竟会去哪里?我是否需要包括所有3或者我可以包括1,2,20?
Kar*_*iya 485
谷歌的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.
Run Code Online (Sandbox Code Playgroud)
并非所有类型都始终由异步任务使用.要将类型标记为未使用,只需使用类型Void:
private class MyTask extends AsyncTask<Void, Void, Void> { ... }
Run Code Online (Sandbox Code Playgroud)
您可以进一步参考:http://developer.android.com/reference/android/os/AsyncTask.html
或者您可以通过引用Sankar-Ganesh的博客来清楚AsyncTask的作用
private class MyTask extends AsyncTask<X, Y, Z>
protected void onPreExecute(){
}
Run Code Online (Sandbox Code Playgroud)
在启动新线程之前执行此方法.没有输入/输出值,因此只需初始化变量或您认为需要做的任何事情.
protected Z doInBackground(X...x){
}
Run Code Online (Sandbox Code Playgroud)
AsyncTask类中最重要的方法.你必须在这里放置你想要在后台做的所有东西,与主要的不同.这里我们有一个输入值来自"X"类型的对象数组(你在标题中看到了吗?我们有"...... extends AsyncTask"这些是输入参数的类型)并从类型中返回一个对象"Z".
protected void onProgressUpdate(Y y){
}
Run Code Online (Sandbox Code Playgroud)
使用方法publishProgress(y)调用此方法,并且通常在要在主屏幕中显示任何进度或信息时使用它,例如显示您在后台执行操作的进度条的进度条.
protected void onPostExecute(Z z){
}
Run Code Online (Sandbox Code Playgroud)
在后台操作完成后调用此方法.作为输入参数,您将收到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.
Run Code Online (Sandbox Code Playgroud)
我们如何从外部课堂调用此任务?只需以下两行:
MyTask myTask = new MyTask();
myTask.execute(x);
Run Code Online (Sandbox Code Playgroud)
其中x是X类型的输入参数.
一旦我们完成任务,我们就可以从"外部"找到它的状态.使用"getStatus()"方法.
myTask.getStatus();
Run Code Online (Sandbox Code Playgroud)
我们可以获得以下状态:
RUNNING - 表示任务正在运行.
PENDING - 表示任务尚未执行.
FINISHED - 表示onPostExecute(Z)已完成.
关于使用AsyncTask的提示
不要手动调用onPreExecute,doInBackground和onPostExecute方法.这是由系统自动完成的.
您不能在另一个AsyncTask或Thread中调用AsyncTask.方法execute的调用必须在UI Thread中完成.
onPostExecute方法在UI Thread中执行(这里你可以调用另一个AsyncTask!).
任务的输入参数可以是Object数组,这样您就可以放置所需的任何对象和类型.
mri*_*rid 68
我迟到了,但认为这可能对某人有所帮助.
Roh*_*ngh 11
一个AsyncTask是运行在后台线程后台任务。它接受一个Input,执行Progress并给出Output。
即
AsyncTask<Input,Progress,Output>。
在我看来,当我们试图记住表格中的参数时,造成混乱的主要原因是AsyncTask。
关键是不要记住。
如果您可以可视化任务真正需要执行的操作,那么AsyncTask使用正确的签名编写便是小菜一碟。
只需弄清楚您的输入,进度和输出是什么,您就可以轻松进行了。
doInBackgound()方法是最重要的方法,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
}
}
Run Code Online (Sandbox Code Playgroud)
您将如何运行此任务
new DownLoadTask().execute("Paradise.mp3");
Run Code Online (Sandbox Code Playgroud)
请参考以下链接:
您不能传递三个以上的参数,如果只想传递1个参数,则对其他两个参数使用void。
1. private class DownloadFilesTask extends AsyncTask<URL, Integer, Long>
2. protected class InitTask extends AsyncTask<Context, Integer, Integer>
Run Code Online (Sandbox Code Playgroud)
异步任务由在后台线程上运行的计算定义,并且其结果发布在UI线程上。异步任务由3个通用类型(称为Params,Progress和Result)以及4个步骤(称为onPreExecute,doInBackground,onProgressUpdate和onPostExecute)定义。
KPBird
| 归档时间: |
|
| 查看次数: |
100513 次 |
| 最近记录: |