Back to project page AndroidHttpTutorial.
The source code is released under:
Apache License
If you think the Android project AndroidHttpTutorial listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.yanlu.android.learn.imageloadtutorial.app.async; /*from w w w .j a v a 2s.c o m*/ import android.os.AsyncTask; import android.os.Build; import android.util.Log; import java.util.concurrent.RejectedExecutionException; public abstract class SimpleTask<T> extends AsyncTask<Integer, Long, T> { private static final String TAG = "AsyncTask2<T>"; protected Exception mException = null; /** * ?task??????: ?UI Thread??? */ protected void onTaskBefore() {} /** * task??: ?work Thread??? */ protected abstract T onTask() throws Exception; /** * task?????????UI Thread??? */ protected abstract void onTaskAfter(T t) throws Exception; /** * onTask()???????. */ public void publishProgressPublic(Long... values) { publishProgress(values); } /** * ??????... */ protected void onHandleAkException(Exception e) { Log.w(TAG, e.toString()); } @Override protected void onPreExecute() { onTaskBefore(); } private AsyncTask<Integer, Long, T> startOnSerial() { //???? ??RejectedExecutionException try { return execute(0); } catch (RejectedExecutionException e) { return null; } } public AsyncTask<Integer, Long, T> start() { //??RejectedExecutionException try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { return executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 0); } else { return execute(0); } } catch (RejectedExecutionException e) { return null; } } @Override protected T doInBackground(Integer... integers) { try { if (mException == null) { return onTask(); } else { return null; } } catch (Exception e) { mException = e; return null; } } @Override protected void onPostExecute(T t) { if (mException != null) { onHandleAkException(mException); } else { try { onTaskAfter(t); } catch (Exception akException) { onHandleAkException(mException); } } } }