Skip to content

Instantly share code, notes, and snippets.

@mancdevcarl
Last active December 18, 2015 05:59
Show Gist options
  • Save mancdevcarl/5737023 to your computer and use it in GitHub Desktop.
Save mancdevcarl/5737023 to your computer and use it in GitHub Desktop.
AsyncTask Callback Example
public class Act extends Activity {
OnTaskCompleted otc = new OnTaskCompleted() {
@Override
public void onTaskCompleted(String result) {
Log.d("onTaskCompleted", result);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Async a = new Async(otc);
a.execute();
}
}
public interface OnTaskCompleted {
void onTaskCompleted(String result);
}
public class Async extends AsyncTask<String, Void, String> {
private OnTaskCompleted listener;
private String result;
public Async(OnTaskCompleted listener) {
this.listener = listener;//
}
@Override
protected String doInBackground(String... arg0) {
//do thread work
return result;
}
protected void onProgressUpdate(Integer... progress) {
}
@Override
protected void onPostExecute(String result) {
try {
listener.onTaskCompleted(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment