如何传递更多的值doInBackground
My AsyncTask
看起来像这样。
private class DownloadFile extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
{
}
}
是否有可能以某种方式向我传递更多的价值观protected String DoInBackground
例如:protected String doInBackground(String... sUrl, String... otherUrl, Context context)
以及如何execute
the AsyncTask
after? new DownloadFile.execute("","",this)
或者其他的东西?
您可以发送多个参数,因为您可以将它们作为可变参数发送。但你必须使用相同类型的参数。因此,要做您正在尝试的事情,您可以遵循以下任何操作
Option 1
您可以使用 setter 方法来设置类成员的某些值,然后在 doInBackGround 中使用这些值。例如
private class DownloadFile extends AsyncTask<String, Integer, String> {
private Context context;
public void setContext(Context c){
context = c;
}
@Override
protected String doInBackground(String... sUrl) {
{
// use context here
}
}
Option 2
或者您可以使用构造函数来传递值,例如
private class DownloadFile extends AsyncTask<String, Integer, String> {
private Context context;
public DownloadFile (Context c){
context = c;
}
@Override
protected String doInBackground(String... sUrl) {
{
// use context here
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)