what does the three dots in “doInBackground(URL… urls)” mean?

本文深入解析了Android中AsyncTask类中URL参数的用途与工作原理,详细解释了如何通过URL参数执行后台任务并进行进度更新与结果处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 (urls holds more than oneURL).

This is typically used to allow users of the AsyncTask to 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 yourDownloadFilesTask like 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 toonProgressUpdate().

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 Long generic 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值