Mat*_*ieu 12 android android-asynctask
大家.我在android网站上读到了AsyncTask的excute()的描述:
public final AsyncTask<Params, Progress, Result> execute (Params... params)
但在我的项目中,我已经阅读了这样的代码:
private static final int JSON = 0;
private static final int NAME = 1;
@Override
protected String doInBackground(String... strData) {
FileOutputTask task = new FileOutputTask(context);
task.execute(strData[JSON], strData[NAME]);
}
Run Code Online (Sandbox Code Playgroud)
有人能告诉我为什么有2个输入参数的执行()方法?
由于根据规范,应该只有一个输入参数.
提前致谢!
Ant*_*ios 44
这是我如何通过多个参数传递它.你可以像鲍里斯所描述的那样做,但如果你传递不同的类型怎么办?
首先,像往常一样创建AsyncTask,但在其中创建一个构造函数:
private class StartTask extends AsyncTask<Context, Void, Boolean>
{
private ProgressDialog progress;
private String strAction="";
public StartTask(ProgressDialog progress, String Action)
{
this.progress = progress;
this.strAction = Action;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在您的活动或其他任何事情上,当您想要启动操作时,您可以调用AsyncTask并传递任意数量的参数.
ProgressDialog progress = new ProgressDialog(this);
progress.setMessage("Loading...");
String strAction = "this_is_a_string";
new StartTask(progress, strAction).execute(this);
Run Code Online (Sandbox Code Playgroud)
您可以看到调用"StartTask"并传递constuctor参数现在将在StartTask类中分配变量.
Bor*_*vić 13
读Params... params为Params[] params.您可以根据需要发送尽可能多的参数.
| 归档时间: |
|
| 查看次数: |
18521 次 |
| 最近记录: |