Android项目-高考作文-AsyncTask的不足
2014-11-02 21:05
260 查看
1, AsyncTask的不足.
从android4.0开始, 后台只允许一个AsyncTask执行, 如果当前的AsyncTask没有执行完毕, 那么当前的请求一直处于等待状态. 直到上一个执行完毕. 并且还不能停止上一个AsyncTask. 但是为了快速响应应用的请求, 我们需要多个任务并行执行.
2, 基于AsyncTask的源码 改写自己的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; } } }
相关文章推荐
- Android项目-高考作文-AsyncTask的不足
- Android项目-高考作文功能简介(一)
- Android项目-高考作文-抽象BaseAdapter
- Android项目-高考作文项目架构(二)
- Android项目-高考作文项目架构(二)
- Android项目-高考作文-使用ORMLite抽象公共的Dao层
- Android项目-高考作文项目架构(三)
- Android项目-高考作文-抽象BaseAdapter
- Android项目-高考作文功能简介(一)
- Android项目-高考作文-使用ORMLite抽象公共的Dao层
- Android项目-高考作文项目架构(三)
- Android项目----AsyncTask异步操作
- 开源异步处项目Droid-Fu详解 For Android: BetterActivity, BetterService And BetterAsyncTask
- Android中如何处理耗时操作,AsyncTask有什么不足之处.
- Android利用AsyncTask异步加载网络图片,详细教程,图+代码+文字+项目
- 个人项目-密码管理-总结三(Android的AsyncTask执行完成后通知UI及下拉刷新)
- 慕课网:Android异步加载AsyncTask项目实战中
- android AsyncTask 在项目中的特殊用法
- 慕课网:Android异步加载AsyncTask项目实战上
- 上海高考惊现0分作文 只因描写同性恋题材