私はAsyncTask
かなり使ってきましたが、私を混乱させる一見単純な質問に出くわしました。質問はこれです:
publishProgress(Progress... values)
すぐに戻ることになっていますか?つまり、このメソッドは非同期ですか?
いくつかのコンテキスト:
次のコードかどうかを判断しようとしています
- 応答に関係なく、3 秒ごとに HTTP 要求を起動するOR
- HTTP 要求を発行し、応答を待ってから、次の要求を発行する前に 3 秒間スリープします。
パブリック クラス MyAsyncTask {
@Override
protected void doInBackground(String... params) {
while (mRunning) {
// Call publishProgress
this.publishProgress(makeHttpRequest());
// Sleep for 3 seconds
synchronized (lock) {
try {
lock.wait(3 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
return null;
}
private HttpResponse makeHttpRequest() {
HttpResponse ajaxResponse = null;
Throwable throwable = null;
try {
ajaxResponse = mHttpClient.execute(mHttpGet);
} catch (ClientProtocolException e) {
// Handle exception
} catch (IOException e) {
// Handle exception
} catch (Exception e) {
// Handle exception
}
return ajaxResponse;
}
@Override
protected void onProgressUpdate(HttpResponse... values) {
// Do something with the response
}
}