Android AsyncTask
2010-08-31 00:25:00 来源:WEB开发网private final FutureTask
constructor
initialize mWorker, mFuture.
execute()
onPreExecute(); // run in UI thread
mWorker.mParams = params;
sExecutor.execute(mFuture);
then invoke
call() in mWorker = new ...
mWorker = new WorkerRunnable
public Result call() throws Exception {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
return doInBackground(mParams); // run in new thread
}
};
then invoke
done() in mFuture = new ...
mFuture = new FutureTask
@Override
protected void done() {
Message message;
Result result = null;
try {
result = get();
} catch (InterruptedException e) {
android.util.Log.w(LOG_TAG, e);
} catch (ExecutionException e) {
throw new RuntimeException("An error occured while executing doInBackground()",
e.getCause());
} catch (CancellationException e) {
message = sHandler.obtainMessage(MESSAGE_POST_CANCEL,
new AsyncTaskResult
message.sendToTarget();
return;
} catch (Throwable t) {
throw new RuntimeException("An error occured while executing "
+ "doInBackground()", t);
}
message = sHandler.obtainMessage(MESSAGE_POST_RESULT,
new AsyncTaskResult
message.sendToTarget();
}
};
Let us look up inner class InternalHandler
private static class InternalHandler extends Handler {
@SuppressWarnings({"unchecked", "RawUseOfParameterizedType"})
更多精彩
赞助商链接