使用回调接口或带有抽象回调方法的抽象类。
回调接口示例:
public class SampleActivity extends Activity { //define callback interface interface MyCallbackInterface { void onDownloadFinished(String result); } //your method slightly modified to take callback into account public void downloadUrl(String stringUrl, MyCallbackInterface callback) { new DownloadWebpageTask(callback).execute(stringUrl); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //example to modified downloadUrl method downloadUrl("http://google.com", new MyCallbackInterface() { @Override public void onDownloadFinished(String result) { // Do something when download finished } }); } //your async task class private class DownloadWebpageTask extends AsyncTask<String, Void, String> { final MyCallbackInterface callback; DownloadWebpageTask(MyCallbackInterface callback) { this.callback = callback; } @Override protected void onPostExecute(String result) { callback.onDownloadFinished(result); } //except for this leave your pre for this class untouched... }}第二种选择更加简洁。您甚至
onPostExecute不必完全按照需要定义“
onDownloaded事件”的抽象方法。只需
DownloadWebpageTask在您的
downloadUrl方法内部添加一个匿名内联类即可。
//your method slightly modified to take callback into account public void downloadUrl(String stringUrl, final MyCallbackInterface callback) { new DownloadWebpageTask() { @Override protected void onPostExecute(String result) { super.onPostExecute(result); callback.onDownloadFinished(result); } }.execute(stringUrl); } //...


