1, AsyncTask的不足.
从android4.0开始, 后台只允许一个AsyncTask执行, 如果当前的AsyncTask没有执行完毕, 那么当前的请求一直处于等待状态. 直到上一个执行完毕. 并且还不能停止上一个AsyncTask. 但是为了快速响应应用的请求, 我们需要多个任务并行执行.
2, 基于AsyncTask的源码 改写自己的AsyncTask.
public abstract class AsyncTaskCustomize<Params, Result> { private static final int MESSAGE_POST_RESULT = 0; private Thread thread; private InternalHandler mHandler = new InternalHandler(); private final AtomicBoolean mCancelled = new AtomicBoolean(); private volatile Status mStatus = Status.PENDING; private WorkerRunnable<Params, Result> work; protected int key; public AsyncTaskCustomize(int key) { this.key = key; work = new WorkerRunnable<Params, Result>() { @Override public void run() { Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); postResult(doInBackground(mParams)); } }; thread = new Thread(work); } public final boolean isCancelled() { return mCancelled.get(); } private void postResult(Result result) { @SuppressWarnings("unchecked") Message message = mHandler.obtainMessage(MESSAGE_POST_RESULT, new AsyncTaskResult<Result>(this, result)); message.sendToTarget(); // return result; } public final AsyncTaskCustomize<Params, Result> execute(Params... params) { return executeTask(params); } /** * @param params */ private AsyncTaskCustomize<Params, Result> executeTask(Params... params) { if (mStatus != Status.PENDING) { switch (mStatus) { case RUNNING: throw new IllegalStateException("Cannot execute task:" + " the task is already running."); case FINISHED: throw new IllegalStateException("Cannot execute task:" + " the task has already been executed " + "(a task can be executed only once)"); default: break; } } onPreExecute(); work.mParams = params; mStatus = Status.RUNNING; thread.start(); return this; } /** * must add super.onPreExecute(result); */ public void onPreExecute() { TaskController.getInstance().add(key, this); } /** * must add super.onPostExecute(result); */ public void onPostExecute(Result result) { TaskController.getInstance().remove(key); } public abstract Result doInBackground(Params... params); private static final class InternalHandler extends Handler { @SuppressWarnings("unchecked") @Override public void handleMessage(Message msg) { @SuppressWarnings("rawtypes") AsyncTaskResult result = (AsyncTaskResult) msg.obj; switch (msg.what) { case MESSAGE_POST_RESULT: // There is only one result result.mTask.finish(result.mData[0]); break; } } } private void finish(Result result) { if (isCancelled()) { onCancelled(result); } else { onPostExecute(result); } mStatus = Status.FINISHED; } public final boolean cancel() { if (isCancelled()) { return false; } if (mStatus == Status.FINISHED) { return false; } try { thread.interrupt(); mCancelled.set(true); } catch (Exception e) { e.printStackTrace(); return false; } return true; } /** * @param result */ protected void onCancelled(Result result) { } protected void onCancelled() { } private static abstract class WorkerRunnable<Params, Result> implements Runnable { Params[] mParams; } public enum Status { /** * Indicates that the task has not been executed yet. */ PENDING, /** * Indicates that the task is running. */ RUNNING, /** * Indicates that {@link AsyncTask#onPostExecute} has finished. */ FINISHED, } @SuppressWarnings("rawtypes") private static class AsyncTaskResult<Data> { final AsyncTaskCustomize mTask; final Data[] mData; AsyncTaskResult(AsyncTaskCustomize task, Data... data) { mTask = task; mData = data; } } }