What's AsyncTask? What's the difference between Thread and AsyncTask?
AsyncTask This class allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most).
AsyncTask 就是 thread+handler+线程池。
AsyncTask has different runtime behavior on different SDK version: run serially or parallel.
new AsyncTask<URL, Integer, Long>() {
// Override this method to perform a computation on a background thread.
@Override
protected Long doInBackground(URL... urls) {
publishProgress(50);
return downloadedSize;
}
// Runs on the UI thread after {@link #publishProgress} is invoked.
@Override
protected void onProgressUpdate(Integer... progress) {
}
// Runs on the UI thread after {@link #doInBackground}.
@Override
protected void onPostExecute(Long size) {
}
}