0

I'm trying to understand Googles sample code for the AsynckTask class. On the line private class DownloadFilesTask extends AsyncTask I'm assuming that Params is type URL, Progress is type integer and Result is type long.

I don't understand the following line protected Long doInBackground(URL... urls)

Google's sample code:

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }

}

Ted pottel
  • 6,869
  • 21
  • 75
  • 134
  • 6
    "I don't understand the following line protected Long doInBackground(URL... urls)" -- please explain in greater detail what you do not understand. That is a method declaration in a Java class, one that happens to use [a varargs parameter](http://java-demos.blogspot.in/2012/12/java-varargs-tutorial.html). – CommonsWare Jul 15 '15 at 22:25
  • Are you referring to the fact that URL is an argument list? http://www.deitel.com/articles/java_tutorials/20060106/VariableLengthArgumentLists.html – Phuong Nguyen Jul 15 '15 at 22:46
  • I agree. "return totalSize" to where? What receives the value returned from doInBackground? – Philip Young Jul 30 '21 at 17:04

2 Answers2

0

If you are confused by the parameter (URL... urls), I suggest you see this question. Basically, it means that zero or more URLs can be used as parameters for the method.

Community
  • 1
  • 1
frgnvola
  • 518
  • 3
  • 16
0

I don't understand the following line protected Long doInBackground(URL... urls)

That means: when you will be executing the task you can pass to the AsynckTask.execute() method as many urls as you need:

new DownloadFilesTask().execute();

or

new DownloadFilesTask().execute(url);

or

new DownloadFilesTask().execute(url_1, url_2);

and so on...

Andrew
  • 36,676
  • 11
  • 141
  • 113