2013-05-28 125 views
8

傳遞更多的價值如何在doInBackground在doInBackground的AsyncTask的Android

傳遞更多的價值我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)

而如何executeAsyncTasknew DownloadFile.execute("","",this)什麼的?

+1

您可以創建參數化構造函數DownloadFile類,然後在類體中的任何位置使用這些參數。 –

回答

7
String... sUrl 

連續三個點意味着更多然後一個字符串值。 dots are varargs

如何傳遞上下文?

您可以強制將它添加一個構造函數上下文作爲參數:

private Context mContext; 
public void setContext(Context context){ 
    if (context == null) { 
     throw new IllegalArgumentException("Context can't be null"); 
    } 
    mContext = context; 
} 
+1

以及如何傳遞上下文? – Naskov

11

您可以發送多個參數,因爲您可以將它們發送爲可變參數。但是你必須使用相同類型的參數。所以,做你想你可以按照以下任

選項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 
     } 
} 

選項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 
     } 
} 
+0

幹得好,謝謝! –

1

就可以使用構造

private class DownloadFile extends AsyncTask<String, Integer, String> { 
      private Context context; 
      public void DownloadFile(Context c,String one, int two){ 
       context = c; 
      } 
      @Override 
      protected String doInBackground(String... sUrl) { 
      { 
        // use context here 
      } 
    } 
3

是的,你可以在constructor傳遞更多的價值,但不doInBackground

嘗試這種方式

new DownloadFile(String sUrl,String other Url,Context context).execute(); 

異步任務

private class DownloadFile extends AsyncTask<String, Integer, String> { 

    public DownloadFile(String url,String url2,Context ctx) 
    { 

    } 
    @Override 
    protected String doInBackground(String... sUrl) { 
    { 

    } 

}

3

你不能,有以下原因:

protected String doInBackground(String... sUrl, String... otherUrl, Context context) 

不是有效的方法簽名。點符號(Varargs)只能用作方法的最後一個參數。這種限制是因爲否則會使多態性變得更加複雜。事實上,java如何知道你的字符串是sUrl還是otherUrl

此外,doInBackground覆蓋AsyncTask的方法。因此,您不能更改方法簽名。

但是,您可以做的是讓您的班級的成員值通過您的DownloadFile班的構造函數,或者在調用execute之前添加設置者來設置它們。

+0

+1爲好的解釋 – stinepike

1
new DownloadFile().execute(Str1,str2,str3,........); 

這是通過多個URL ...如果你想在doinbackground方法發送更多價值的一種方式..

public class DownloadFile extends AsyncTask<DataHolders, Integer, String> { 
    public class DataHolders { 
     public String url; 
     public String myval; 
    } 
    @Override 
    protected String doInBackground(DataHolders... params) { 

     return null; 
    } 
} 

你可以調用類

DataHolders mhold = new DataHolders(); 
new DownloadFile().execute(mhold,mhold2,mhold3,........); 
1

YPU可以創建構造函數來傳遞不同的類型參數,也可以使用字符串傳輸字符串...... str

private class DownloadTask extends AsyncTask<String, Integer, String> { 
     private Context context; 
      byte[] byteArray; 
     public DownloadTask (Context c,byte[] byteArray){ 
      context = c; 
      byteArray=byteArray; 
     } 
     @Override 
     protected String doInBackground(String... str) { 
     { 
       // use context here 
    System.out.println(param[0]); 
      } 
    } 



new DownloadTask(context,bytearray).excute("xyz"); 
4

你可以做這樣的事情你doInBackground方法中:

String a = sUrl[0] 
String b = sUrl[1] 

這樣執行的AsyncTask:

new DownloadFile().execute(string1,string2); 

的第一個值:SURL [0]將是一個從字符串1過去了,
surl [1]將傳遞的第二個值即string2!

0
new DownloadFile().execute("my url","other parameter or url");  
private class DownloadFile extends AsyncTask<String, Integer, String> { 
      @Override 
      protected String doInBackground(String... sUrl) { 
      { 
       try { 
        return downloadContent(sUrl[0], sUrl[1]); // call 
       } catch (IOException e) { 
        return "Unable to retrieve data. URL may be invalid."; 
       } 
      } 
    } 
相關問題