Android项目-高考作文-AsyncTask的不足
来源:互联网 发布:wkwebview 注入js方法 编辑:程序博客网 时间:2024/05/14 23:48
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>() {@Overridepublic 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")@Overridepublic void handleMessage(Message msg) {@SuppressWarnings("rawtypes")AsyncTaskResult result = (AsyncTaskResult) msg.obj;switch (msg.what) {case MESSAGE_POST_RESULT:// There is only one resultresult.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> implementsRunnable {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;}}}
1 0
- Android项目-高考作文-AsyncTask的不足
- Android项目-高考作文-抽象BaseAdapter
- Android项目-高考作文-使用ORMLite抽象公共的Dao层
- Android项目-高考作文项目架构(二)
- Android项目-高考作文项目架构(三)
- Android项目-高考作文功能简介(一)
- AsyncTask的不足(待续)
- AsyncTask原理及不足
- 高考作文
- 项目准备不足的原因
- 小偷写的一篇高考作文
- 毛主席的"高考作文"
- Android 的优势与不足
- Android的优点和不足
- android系统签名的不足
- android的优点与不足
- 谈谈学习Android的不足
- Android 的AsyncTask使用
- ACM:最少拦截系统:贪心思想
- 必须写下的项目经历
- Spring整合JSON
- 解决ubuntu下使用界面解压zip压缩包乱码
- ListView—Item的click事件和Item里的button等子控件的click事件同时生效的解决办法
- Android项目-高考作文-AsyncTask的不足
- 程序员求职之道(《程序员面试笔试宝典》)之不想签约,但也不想轻易放弃机会,怎么办?
- 11gR2 Clusterware and Grid Home - What You Need to Know (Doc ID 1053147.1)
- 【机器学习】支持向量机SVM
- 【POJ2253】Frogger 最短路,你想写LCA我不拦你
- 第十周项目1.1 六次计算
- ubuntu下锁定笔记本的触摸板
- 项目管理学习笔记之五.沟通协调能力II
- C++对象模型中的虚拟函数分析