AsyncTask helpers [Android]
When using AsyncTask, there are two things I always end up doing: include some error handling and show a progress dialog while the task is executing. I made some little helper classes for that, that I now use instead of AsyncTask. To use them, override the run, onSuccess and onFailure methods.
import android.os.AsyncTask;
/**
* Helper class for AsyncTask that supports exception handling.
*
* @param <Params>
* The type of parameter passed to the background handler.
*
* @param <Result>
* The result returned by the background handler.
*
* @param <ExceptionType>
* The kind of exception the background handler may throw.
*/
public abstract class AsyncHelper<Params, Result, ExceptionType extends Exception>
extends AsyncTask<Params, Void, AsyncHelper.ResultWrapper<Result, ExceptionType>>
{
/// Wrapper around a result or exception
static class ResultWrapper<Result, ExceptionType>
{
private Result result;
private ExceptionType exception;
private ResultWrapper(Result result, ExceptionType exception)
{
this.result = result;
this.exception = exception;
}
}
@SuppressWarnings("unchecked")
@Override
protected ResultWrapper<Result, ExceptionType> doInBackground(Params... args)
{
try
{
return new ResultWrapper<Result, ExceptionType>(run(args), null);
}
catch(Exception e) // Using generic ExceptionType in a catch block is not allowed
{
return new ResultWrapper<Result, ExceptionType>(null, (ExceptionType) e);
}
}
@Override
protected void onPostExecute(ResultWrapper<Result, ExceptionType> result)
{
if (result.exception == null)
onSuccess(result.result);
else
onFailure(result.exception);
}
/**
* Performs the background task.
*
* @param args
* The parameters passed to execute.
*
* @return
* The return value, passed to onSuccess.
*
* @throws ExceptionType
* An exception, passed to onFailure.
*/
abstract protected Result run(Params...args)
throws ExceptionType;
/**
* Invoked when the background task finishes successfully.
*
* @param result
* The result returned by run.
*/
abstract protected void onSuccess(Result result);
/**
* Invoked when the background task thows an exception.
*
* @param exception
* The exception thrown by run.
*/
abstract protected void onFailure(ExceptionType exception);
}
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
/**
* A background task that shows a cancelable progress dialog while running.
*
* @param <Params>
* The type of parameter passed to the background handler.
*
* @param <Result>
* The result returned by the background handler.
*
* @param <ExceptionType>
* The kind of exception the background handler may throw.
*/
public abstract class AsyncProgressHelper<Params, Result, ExceptionType extends Exception>
extends AsyncHelper<Params, Result, ExceptionType>
{
protected final Activity source;
protected final String title;
protected ProgressDialog progress;
/**
* Constructor.
*
* @param source
* The activity that is requesting the background task.
*
* @param title
* The title of the progress dialog.
*/
public AsyncProgressHelper(Activity source, String title)
{
this.source = source;
this.title = title;
}
protected void onPreExecute()
{
progress = ProgressDialog.show(source, null, title, true, true,
new OnCancelListener()
{
public void onCancel(DialogInterface dialog)
{
cancel(true);
}
});
}
@Override
protected void onPostExecute(ResultWrapper<Result, ExceptionType> result)
{
progress.hide();
super.onPostExecute(result);
}
}
Like other code samples on this page, these are hereby released under the GPL.