一 .AsyncTask:
它本质上就是一个封装了线程池和handler的异步框架,用于异步任务
二 .使用方法
1.三个参数:
public class CheckDraftForSendTask extends SafeAsyncTask<Void, Void, Integer>
void: 在执行AsyncTask时候传入的参数.
void: 执行后台任务是在界面上现实的进度.
Integer: 在执行完成后,如果需要结果返回,结果返回的类型.
@WorkerThread
protected final void publishProgress(Progress... values) {
if (!isCancelled()) {
getHandler().obtainMessage(MESSAGE_POST_PROGRESS,
new AsyncTaskResult<Progress>(this, value)).sendToTarget();
}
}
public void handleMessage\(Message msg\) {
AsyncTaskResult<?> result = \(AsyncTaskResult<?>\) msg.obj;
switch \(msg.what\) {
case MESSAGE\_POST\_RESULT:
// There is only one result
result.mTask.finish\(result.mData\[0\]\);
break;
case MESSAGE\_POST\_PROGRESS://这里处理对应的消息,在调用AsyncTask的onProgressUpdate方法.
result.mTask.onProgressUpdate\(result.mData\);
break;
}
}
2.五个方法:
/\*\*
\* Runs on the UI thread before {@link \#doInBackground}.
\*
\* @see \#onPostExecute
\* @see \#doInBackground
\*/
@MainThread
protected void onPreExecute\(\) {//主线程里面执行
}
/\*\*
\* <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\({"UnusedDeclaration"}\)
@MainThread
protected void onPostExecute\(Result result\) {//主线程里面执行
}
/\*\*
\* Runs on the UI thread after {@link \#publishProgress} is invoked.
\* The specified values are the values passed to {@link \#publishProgress}.
\*
\* @param values The values indicating progress.
\*
\* @see \#publishProgress
\* @see \#doInBackground
\*/
@SuppressWarnings\({"UnusedDeclaration"}\)
@MainThread
protected void onProgressUpdate\(Progress... values\) {//主线程里面执行
}
/\*\*
\* <p>Runs on the UI thread after {@link \#cancel\(boolean\)} is invoked and
\* {@link \#doInBackground\(Object\[\]\)} has finished.</p>
\*
\* <p>The default implementation simply invokes {@link \#onCancelled\(\)} and
\* ignores the result. If you write your own implementation, do not call
\* <code>super.onCancelled\(result\)</code>.</p>
\*
\* @param result The result, if any, computed in
\* {@link \#doInBackground\(Object\[\]\)}, can be null
\*
\* @see \#cancel\(boolean\)
\* @see \#isCancelled\(\)
\*/
@SuppressWarnings\({"UnusedParameters"}\)
@MainThread
protected void onCancelled\(Result result\) {//主线程里面执行
onCancelled\(\);
}
/\*\*
\* 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\);
三 .AsyncTask的注意事项
1.内存泄漏: 非静态的内部类持有外部类的匿名引用造成. / AsynclTask的cancel方法的调用.
2.在Activity onDestory的时候I记得调用AsyncTask的cancel方法.