【发布时间】:2015-07-15 22:24:09
【问题描述】:
我正在尝试了解 Google 的 AsynckTask 类示例代码。 上线私有类 DownloadFilesTask extends AsyncTask 我假设 Params 是 URL 类型,Progress 是 integer 类型,Result 是 long 类型。
我不明白下面这行 protected Long doInBackground(URL... urls)
Google 的示例代码:
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");
}
}
【问题讨论】:
-
"我不明白下面的行 protected Long doInBackground(URL... urls)" -- 请更详细地解释你不明白的地方。这是一个 Java 类中的方法声明,恰好使用了a varargs parameter。
-
你指的是 URL 是一个参数列表吗? deitel.com/articles/java_tutorials/20060106/…
-
我同意。 “返回totalSize”到哪里? doInBackground 返回的值是什么?
标签: android