Skip to content

Instantly share code, notes, and snippets.

@yeonsh
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yeonsh/9438288 to your computer and use it in GitHub Desktop.
Save yeonsh/9438288 to your computer and use it in GitHub Desktop.
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
/**
* Use this class when you provide logics for background execution.
*
* Implement your code in onAsyncTaskExecute().
*
* If you have to reuse your code, define a dedicated task class using BDAsyncTaskSkeleton class.
*
* @author y5h
*/
public class BDAsyncTaskOneTime extends AsyncTask<Object, Void, Bundle> {
private static final String TAG = BDAsyncTaskOneTime.class.getSimpleName();
private BDAsyncTaskListener mListener;
private Context mContext;
private Bundle mBundle;
public BDAsyncTaskOneTime() {
super();
}
public BDAsyncTaskOneTime(Context context, BDAsyncTaskListener listener) {
super();
mContext = context;
mListener = listener;
}
public void setOnTaskListener(BDAsyncTaskListener listener) {
mListener = listener;
}
public void setContext(Context context) {
mContext = context;
}
@Override
protected void onPreExecute() {
if (mListener != null) {
mListener.onAsyncTaskPreExecute(mBundle);
}
}
@Override
protected Bundle doInBackground(Object... params) {
if (params.length > 0) {
mBundle = (Bundle) params[0];
}
if (mBundle == null) {
mBundle = new Bundle();
}
mListener.onAsyncTaskExecute(mBundle);
return mBundle;
}
@Override
protected void onPostExecute(Bundle result) {
if (mListener != null) {
mListener.onAsyncTaskPostExecute(result);
}
mListener = null;
}
}
----------------------
import android.os.Bundle;
public interface BDAsyncTaskListener {
public void onAsyncTaskPreExecute(Bundle args);
/*
* In some cases, we don't need reusable tasks. In this case, caller class
* provides logics to be executed using this method.
*
* If you use reusable tasks, this method is optional.
*/
public void onAsyncTaskExecute(Bundle args);
public void onAsyncTaskPostExecute(Bundle args);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment