【问题标题】:AsyncTask: where does the return value of doInBackground() go?AsyncTask:doInBackground() 的返回值去哪了?
【发布时间】:2011-05-28 05:25:43
【问题描述】:

调用AsyncTask<Integer,Integer,Boolean>时,返回值在哪里:

protected Boolean doInBackground(Integer... params)?

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

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

【问题讨论】:

    标签: android return-value android-asynctask


    【解决方案1】:

    然后该值在 onPostExecute 中可用,您可能需要覆盖它以使用结果。

    这是来自 Google 文档的示例代码 sn-p:

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

    【讨论】:

    • 为了使这个除了记录或显示对话框之外的任何实际用途,您可以在您的 UI(或其他)类中嵌套您扩展 AsyncTask 的类,然后从该类调用一个方法使用onPostExecute 方法中的返回值。
    【解决方案2】:

    您可以通过调用AsyncTask 类的get() 方法来检索受保护的布尔值doInBackground() 的返回值:

    AsyncTaskClassName task = new AsyncTaskClassName();
    bool result = task.execute(param1,param2......).get(); 
    

    但要注意 UI 的响应能力,因为 get() 会等待计算完成并会阻塞 UI 线程
    如果您使用的是内部类,最好在 onPostExecute(Boolean result) 方法中完成这项工作。

    如果您只是想更新 UI,AsyncTask 为您提供两种可能性:

  • 要与doInBackground() 中执行的任务并行更新 UI(例如更新ProgressBar),您必须在doInBackground() 方法中调用publishProgress()。然后你必须在 onProgressUpdate() 方法中更新 UI。
  • 要在任务完成后更新 UI,您必须在 onPostExecute() 方法中进行。
    /** This method runs on a background thread (not on the UI thread) */
    @Override
    protected String doInBackground(String... params) {
        for (int progressValue = 0; progressValue  < 100; progressValue++) {
            publishProgress(progressValue);
        }
    }
    
    /** This method runs on the UI thread */
    @Override
    protected void onProgressUpdate(Integer... progressValue) {
        // TODO Update your ProgressBar here
    }
    
    /**
     * Called after doInBackground() method
     * This method runs on the UI thread
     */
    @Override
    protected void onPostExecute(Boolean result) {
       // TODO Update the UI thread with the final result
    }
    

    这样您就不必关心响应问题。

  • 【讨论】:

    • 这不是正确的方法 .get() 将冻结 UI 你能建议任何其他选项我想使用异步任务的返回结果然后想执行其他网络服务请帮助我
    • 如果你在第五行之后阅读我的帖子,你会看到我建议使用OnPostExecute() 而不是get() 以避免阻塞UI线程。
    • 是的,但我想使用返回值,并且在返回值的基础上我想调用多个 url,那么我能做些什么,请帮助我
    • 然后你必须定义一个返回你想要的类型的AsyncTask。可能是字符串而不是布尔值:private class MyAsyncTask extends AsyncTask&lt;String, int[], String&gt;(最后一个参数定义结果类型)。然后你可以从 onPostExecute(String result) 推断和调用你的 url。
    • 非常有帮助。谢谢!
    猜你喜欢
    • 2014-02-26
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    • 2014-03-15
    相关资源
    最近更新 更多