我無法理解Android中Asynctask參數的使用。Android AsyncTask參數
Android開發者文檔解釋了它,如下所示:
AsyncTask must be subclassed to be used.
The subclass will override at least one method (doInBackground(Params...)),
and most often will override a second one (onPostExecute(Result).)
這裏是子類的實例:
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");
}
}
一旦建立,任務是非常簡單的執行:
new DownloadFilesTask().execute(url1, url2, url3);
對於我的AsyncTask
的擴展,我不需要傳入任何參數,但我需要覆蓋doInBackground()
,onProgressUpdate()
和onPostExecute()
。爲什麼我必須將Void,Void,Void
插入AsyncTask<>
?
這些參數做什麼?
這可能對那些遇到這個問題的人有幫助 - [什麼參數傳入AsyncTask?](http:// stackoverflow。COM /問題/ 6053602 /什麼論點,是繞過成 - asynctaskarg1-ARG2,ARG3) –
Sufian