代码之家  ›  专栏  ›  技术社区  ›  Hamy

AsyncTask:OnPostExecute是否保证在调用OnProgressUpdate之后运行?

  •  2
  • Hamy  · 技术社区  · 15 年前

    我想知道有没有可能打电话给 AsyncTask#onProgressUpdate 之后 AsyncTask#onPostExecute 有人打过电话吗?我正在设置相同的文本 TextView using both of them, and I don't want to set text such as "Done!" and then have it overwritten at a later point by text such as "Almost there - 90%"

    另外,我假设 onProgressUpdate 方法工程类似于 SwingWorker 方法中的多个调用 publishProgress 可能在调用 进行中更新 progress.length ?

    2 回复  |  直到 14 年前
        1
  •  2
  •   Rich Schuler    15 年前

    代码到 publishProgress 是:

    protected final void publishProgress(Progress... values) {
        sHandler.obtainMessage(MESSAGE_POST_PROGRESS,
                new AsyncTaskResult<Progress>(this, values)).sendToTarget();
    }
    

    这个 sendToTarget 调用使用 Handler#sendMessage which the docs say "pushes the message onto the end of the message queue." I understand that to mean that you will not get any out of order updates and that they will stack up. 此外,对于每个 出版进展 呼叫会有相应的 onProgressUpdate 打电话。

        2
  •  -1
  •   Nabil Gardon    14 年前

    嗯,很抱歉,不管之前的回答是什么,我都会回答的: 是的,您可以保证在调用onProgressUpdate()之后调用onPostExecute()。

    为什么?Because the doInBackground is responsible for invoking onProgressUpdate(), so it can't happen outside of it. onPostExecute() is invoked after the result has been retrieved, since it does transmit it to onPostExecute()

    当做