WEB开发网
开发学院手机开发Android 开发 Android AsyncTask 阅读

Android AsyncTask

 2010-08-31 00:25:00 来源:WEB开发网   
核心提示:private final FutureTask mFuture;constructorinitialize mWorker, mFuture.execute()onPreExecute(); // run in UI threadmWorker.mParams = params;sExecutor.execute(m

private final FutureTask mFuture;

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(mWorker) {

@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(AsyncTask.this, (Result[]) null));

message.sendToTarget();

return;

} catch (Throwable t) {

throw new RuntimeException("An error occured while executing "

+ "doInBackground()", t);

}

message = sHandler.obtainMessage(MESSAGE_POST_RESULT,

new AsyncTaskResult(AsyncTask.this, result));

message.sendToTarget();

}

};

Let us look up inner class InternalHandler

private static class InternalHandler extends Handler {

@SuppressWarnings({"unchecked", "RawUseOfParameterizedType"})

Tags:Android AsyncTask

编辑录入:coldstar [复制链接] [打 印]
赞助商链接