我有一个从主线程调用的AsyncTask,我希望在完成时弹出一个对话框。除了将对话框代码放在OnPostExecute()中之外,还有一种方法可以将其放在主活动代码中吗?
感谢。
答案 0 :(得分:0)
您可以使用界面。它简单而且前瞻性: 1-创建一个新界面:
public interface IShowPopup {
public void showPopup(String title, string message);
}
2 - 在您的活动中实现该界面:
... MyActivity extends Activity implements IShowPopup {
...
public void showPopup(String title, String message) {
// create a DialogAlert here.
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
builder.setMessage(message);
builder.setTitle(R.string.app_license_title);
AlertDialog dialog = builder.create();
// show dialog.
dialog.show();
}
...
}
3 - 在您的任务中,您保留活动的实例:
...MyTask extends AsyncTask<...> {
private IShowPopup iShowPopup ;
// get the interface from constructor.
public MyTask(IShowPopup isp) {
this.iShowPopup = isp;
}
4 - 使用onPostExecute中的界面:
@Override
public void onPostExecute(??) {
// get some title and message.
iShowPopup.showPopup(title, message);
}
那应该是它!