AsyncTask的:doInBackground在哪里返回值()去?返回值、AsyncTask、doInBackground

2023-09-12 21:44:11 作者:守一座空城℡

在一个AsyncTask的

in a AsyncTask

在那里去的返回值 保护布尔doInBackground(整数... PARAMS)

通常我们开始AsyncTask的使用 新AsyncTaskClassName()执行(参数1,参数2 ......); 不返回任何值

usually we start AsyncTask with new AsyncTaskClassName().execute(param1,param2......); which doesn't return any value

推荐答案

该值就可以在onPostExecute您可能希望以致使工作覆盖。

The value is then available in onPostExecute which you may want to override in order to work with the result.

下面是来自谷歌的文档例如code片断:

Here is example code snippet from Google's docs:

 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));
          }
          return totalSize;
      }

      protected void onProgressUpdate(Integer... progress) {
          setProgressPercent(progress[0]);
      }

      protected void onPostExecute(Long result) {
          showDialog("Downloaded " + result + " bytes");
      }
 }