Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass more values in doInBackground AsyncTask Android

Tags:

android

How to pass more values in the doInBackground

My AsyncTask looks like this.

private class DownloadFile extends AsyncTask<String, Integer, String> {
        @Override
        protected String doInBackground(String... sUrl) {
        {

        }
}

Is it possible somehow to pass more values on my protected String DoInBackground

for example: protected String doInBackground(String... sUrl, String... otherUrl, Context context)

And how to execute the AsyncTask after? new DownloadFile.execute("","",this) or something?

like image 715
Naskov Avatar asked May 28 '13 09:05

Naskov


4 Answers

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.";
                }
            }
    }
like image 67
emdy Avatar answered Oct 22 '22 16:10

emdy


you can do something like this inside your doInBackground method:

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

execute AsyncTask in this way:

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

the first value : sUrl[0] will be the one passed from string1 and
surl[1] will be the second value passed i.e string2 !

like image 36
Niraj Adhikari Avatar answered Oct 22 '22 15:10

Niraj Adhikari


you can send multiple parameters as you can send them as varargs. But you have to use same Type of parameter. So to do what you are trying you can follow any of the followings

Option 1

you can use a setter method to set some value of the class member then use those in doInBackGround. For example

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

Or you can use constructor to pass the values like

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
        }
}
like image 39
stinepike Avatar answered Oct 22 '22 17:10

stinepike


String... sUrl

the three consecutive dots meaning more then one String. The dots are varargs

And how to pass the Context?

you can force it adding a constructor that takes the Context as parameter:

private Context mContext;
public void setContext(Context context){
    if (context == null) {
      throw new IllegalArgumentException("Context can't be null");
    }
    mContext = context;
}
like image 24
Blackbelt Avatar answered Oct 22 '22 17:10

Blackbelt