的返回值去哪里了

的返回值去哪里了

本文介绍了AsyncTask:doInBackground() 的返回值去哪里了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用AsyncTask时,返回值是:

protected Boolean doInBackground(Integer... params)?

通常我们用 new AsyncTaskClassName().execute(param1,param2......); 启动 AsyncTask 但它似乎没有返回值.

Usually we start AsyncTask with new AsyncTaskClassName().execute(param1,param2......); but it doesn't appear to return a value.

doInBackground() 的返回值在哪里可以找到?

Where can the return value of doInBackground() be found?

推荐答案

该值随后在 onPostExecute 您可能想要覆盖它以处理结果.

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

以下是 Google 文档中的示例代码片段:

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");
      }
 }

这篇关于AsyncTask:doInBackground() 的返回值去哪里了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:40