在我的兩個類裡面,一個是主Activity
,另一個繼承了AsyncTask
。我想在主Activity
中獲取AsyncTask
中的OnPostExecute
結果,怎麼傳遞給主Activity呢?
很簡單。
先創建interface:
public interface AsyncResponse {
void processFinish(String output);
}
在你的asynchronous類中調用:
public class OverlayTask extends AsyncTask{
public AsyncResponse delegate=null;
@Override
protected void onPostExecute(Void unused) {
delegate.processFinish((String)output);
}
然後在主Activity需要implements AsyncResponse
public class MainActivity implements AsyncResponse{
OverlayTask task=null;
@Override
public void onCreate(Bundle savedInstanceState) {
task = new OverlayTask();
task.delegate = this;
}
void processFinish(String output){
}
}