what does the three dots in “doInBackground(URL… urls)” mean?
2013-09-11 23:23
495 查看
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"); } }
As Morrison said, the
...syntax
is for a variable length list of arguments (
urlsholds
more than one
URL).
This is typically used to allow users of the
AsyncTaskto
do things like (in your case) pass in more than one URL to be fetched in the background. If you only have one URL, you would use your
DownloadFilesTasklike
this:
DownloadFilesTask worker = new DownloadFilesTask(); worker.execute(new URL("http://google.com"));
or with multiple URLs, do this:
worker.execute(new URL[]{ new URL("http://google.com"), new URL("http://stackoverflow.com") });
The
onProgressUpdate()is
used to let the background task communicate progress to the UI. Since the background task might involve multiple jobs (one for each URL parameter),
it may make sense to publish separate progress values (e.g. 0 to 100% complete) for each task. You don't have to. Your background task could certainly choose to calculate a total progress
value, and pass that single value to
onProgressUpdate().
The
onPostExecute()method
is a little different. It processes a single result, from the set of operations that were done in
doInBackground().
For example, if you download multiple URLs, then you might return a failure code if any of them failed. The input parameter to
onPostExecute()will
be whatever value you return from
doInBackground().
That's why, in this case, they are both
Longvalues.
If
doInBackground()returns
totalSize,
then that value will be passed on
onPostExecute(),
where it can be used to inform the user what happened, or whatever other post-processing you like.
If you really need to communicate multiple results as a result of your background task, you can certainly change the
Longgeneric
parameter to something other than a
Long(e.g.
some kind of collection).
From Stackoverflow http://stackoverflow.com/questions/12665769/what-does-the-three-dots-in-doinbackgroundurl-urls-mean
相关文章推荐
- What does the three dots in react do?
- What does the distance attribute in DMatches mean?
- So, what does the asterisk(*) mean in AssemblyVersion("1.0.*")?
- What does the 11 mean in INT(11)?
- What does idl attribute mean in the W3C html5 standard document?
- What does the first letter "c" mean in "calloc"?
- What does the [Flags] Enum Attribute mean in C#?
- Build a Simple Program Using Boost in(std::cin) : What does it mean?
- What does the flowchart look like in top Journals?
- What does it mean by rendering of page in JSF
- The usage of the Javascript method call and apply(what is the new and object.create difference.)(What does call(null) mean?)
- What happens when you type an URL in the browser and press enter?
- What does => and () => mean in Scala
- What does “=>” mean in import in scala?(转自StackOverflow问答)
- What does Using filesort mean in MySQL?
- What does wildcard address in InetSocketAddress mean?
- What does the yield keyword do in Python?
- What does var that = this; mean in javascript?
- What does information_schema.TABLES.DATA_FREE mean in MySQL?
- What does it mean to be an Open Source author? A story from the inside