Skip to content

Instantly share code, notes, and snippets.

@ytkhs
Created March 21, 2011 13:01
Show Gist options
  • Save ytkhs/879416 to your computer and use it in GitHub Desktop.
Save ytkhs/879416 to your computer and use it in GitHub Desktop.
a sample AsyncTask for Android
SampleTask task = new SampleTask(this);
task.execute(stringValues);
//check task status
if(task.getStatus() != AsyncTask.Status.FINISHED) {
if(!task.isCancelled) {
//call SampleTask.onCancelled()
task.cancel();
}
}
public class SampleTask extends AsyncTask<String, Integer, Object> {
//fields
private ProgressDialog mProgressDialog;
//constructors
public SampleTask() {
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Object doInBackground(String... stringValues) {
//for onProgressUpdate()
Integer progress = 10;
publishProgress(progress);
return result;
}
@Override
protected void onPostExecute(Object result) {
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(Integer... progress) {
mProgressDialog.setProgress(progress[0]);
}
@Override
protected void onCancelled() {
super.onCancelled();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment