Last active
September 11, 2015 02:27
-
-
Save KazaKago/676d9b447ca3dc302984 to your computer and use it in GitHub Desktop.
AsyncTaskの汎用基底クラス
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.AsyncTask; | |
/** | |
* AsyncTaskの基底クラス | |
* | |
* @param <Params> | |
* @param <Progress> | |
* @param <Result> | |
* @author Kensuke | |
*/ | |
public abstract class BaseAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> { | |
/** | |
* 呼び出し元スレッドへ返却するインターフェース | |
* | |
* @param <Progress> | |
* @param <Result> | |
* @author Kensuke | |
*/ | |
public static interface AsyncTaskCallbackListener<Progress, Result> { | |
/** | |
* AsyncTask#onPreExecute | |
* | |
* @param tag | |
*/ | |
public void onPreExecute(int tag); | |
/** | |
* AsyncTask#onProgressUpdate | |
* | |
* @param tag | |
* @param values | |
*/ | |
public void onProgressUpdate(int tag, Progress... values); | |
/** | |
* AsyncTask#onCancelled | |
* | |
* @param tag | |
*/ | |
public void onCancelled(int tag); | |
/** | |
* AsyncTask#onPostExecute | |
* | |
* @param tag | |
* @param result | |
*/ | |
public void onPostExecute(int tag, Result result); | |
} | |
/** | |
* タグ | |
*/ | |
protected int mTag; | |
/** | |
* 呼び出し元スレッドへ返却するインターフェース | |
*/ | |
protected AsyncTaskCallbackListener<Progress, Result> mListener; | |
public BaseAsyncTask() { | |
} | |
public BaseAsyncTask(int tag) { | |
super(); | |
mTag = tag; | |
} | |
@Override | |
protected void onPreExecute() { | |
super.onPreExecute(); | |
if (mListener != null) mListener.onPreExecute(mTag); | |
} | |
@Override | |
protected abstract Result doInBackground(Params... params); | |
@Override | |
protected void onProgressUpdate(Progress... values) { | |
super.onProgressUpdate(values); | |
if (mListener != null) mListener.onProgressUpdate(mTag, values); | |
} | |
@Override | |
protected void onCancelled() { | |
super.onCancelled(); | |
if (mListener != null) mListener.onCancelled(mTag); | |
} | |
@Override | |
protected void onPostExecute(Result result) { | |
super.onPostExecute(result); | |
if (mListener != null) mListener.onPostExecute(mTag, result); | |
} | |
/** | |
* タグをセットする | |
* | |
* @param tag | |
*/ | |
public void setTag(int tag) { | |
mTag = tag; | |
} | |
/** | |
* タグを取得する | |
* | |
* @return | |
*/ | |
public int getTag() { | |
return mTag; | |
} | |
/** | |
* コールバックリスナーをセットする | |
* | |
* @param listener | |
*/ | |
public void setCallbackListener(AsyncTaskCallbackListener<Progress, Result> listener) { | |
mListener = listener; | |
} | |
/** | |
* コールバックリスナーを解除する | |
*/ | |
public void removeCallbackListener() { | |
mListener = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment