首先是AsyncTask
这是一个用于异步处理数据的线程操作类。由于Android中存在UI线程的概念,只有在UI线程中才能对View(肉眼可见的地方)进行修改和调整。UI线程中是不能做一些耗时地操作的,比如说访问网络(http请求)、从SD卡读写数据(I/O操作)亦或者是比较复杂的数据运算,这些都是不能放在UI线程中进行的(会导致ANR异常)。
为了解决这个问题,Android系统提供了这个异步操作类。(以下是部分代码)
public abstract class AsyncTask<Params, Progress, Result> {
/**
* Override this method to perform a computation on a background thread. The
* specified parameters are the parameters passed to {@link #execute}
* by the caller of this task.
*
* This method can call {@link #publishProgress} to publish updates
* on the UI thread.
*
* @param params The parameters of the task.
*
* @return A result, defined by the subclass of this task.
*
* @see #onPreExecute()
* @see #onPostExecute
* @see #publishProgress
*/
@WorkerThread
protected abstract Result doInBackground(Params... params);
/**
* <p>Runs on the UI thread after {@link #doInBackground}. The
* specified result is the value returned by {@link #doInBackground}.</p>
*
* <p>This method won't be invoked if the task was cancelled.</p>
*
* @param result The result of the operation computed by {@link #doInBackground}.
*
* @see #onPreExecute
* @see #doInBackground
* @see #onCancelled(Object)
*/
@SuppressWarnings