Created
March 21, 2011 13:01
-
-
Save ytkhs/879416 to your computer and use it in GitHub Desktop.
a sample AsyncTask for Android
This file contains hidden or 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
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