gpt4 book ai didi

安卓异步任务参数

转载 作者:太空宇宙 更新时间:2023-11-03 13:51:31 25 4
gpt4 key购买 nike

我无法理解 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<>

参数有什么作用?

最佳答案

从文档中可以看出,使用 Void 只是将类型标记为未使用。您不必在 AsyncTask 中输入类型.

The three types used by an asynchronous task are the following:

  1. Params, the type of the parameters sent to the task upon execution.
  2. Progress, the type of the progress units published during the background computation.
  3. Result, the type of the result of the background computation.

Not all types are always used by an asynchronous task. To mark a type as unused, simply use the type Void:

private class MyTask extends AsyncTask<Void, Void, Void> { ... }

关于安卓异步任务参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34486469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com