Created
July 5, 2014 09:07
-
-
Save ed-george/8097940a2b2a55d036c5 to your computer and use it in GitHub Desktop.
Async Callback Example
This file contains 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
/** | |
* @author edgeorge | |
* | |
*/ | |
public interface AsyncResponse { | |
void onProcessFinish(String result, int id); | |
} |
This file contains 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
import android.os.AsyncTask; | |
public class ExampleAsync extends AsyncTask<String, Void, String> { | |
private int id; | |
public AsyncResponse delegate = null; | |
public ExampleAsync(int id) { | |
this.id = id; | |
} | |
@Override | |
protected String doInBackground(String... params) { | |
//do work here | |
return null; | |
} | |
@Override | |
protected void onPostExecute(String result) { | |
super.onPostExecute(result); | |
//call delegate | |
delegate.onProcessFinish(result, id); | |
} | |
} |
This file contains 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
public class Test extends Activity implements AsyncResponse{ | |
private static final int ASYNC_ONE = 1; //ID for async | |
@Override | |
protected void onCreate(Bundle arg0) { | |
//other activity stuff before this | |
ExampleAsync async = new ExampleAsync(ASYNC_ONE); | |
async.delegate = this; | |
async.execute(); | |
} | |
@Override | |
public void onProcessFinish(String result, int id) { | |
if(id == ASYNC_ONE){ | |
// process result | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment