Last active
December 18, 2015 05:59
-
-
Save mancdevcarl/5737023 to your computer and use it in GitHub Desktop.
AsyncTask 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
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(); | |
} | |
} |
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 interface OnTaskCompleted { | |
void onTaskCompleted(String result); | |
} |
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 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