我的 ProgressDialog 适用于我在我的 oncreate 方法中使用 show() 的情况。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pd = ProgressDialog.show(this, "Working", "Retreiving Neaby Restaurants", true, true); // initializes the progress dialog
pd.setCanceledOnTouchOutside(false); // on newer versions of android touching outside of the screen will close the dialog. I decided to make it only cancelable when the back button is pressed.
setContentView(R.layout.nearby_places_list);
但是,如果我在异步任务中使用它,对话框代码将不起作用,因为它不在 on create() 中。争论显示(this, "Working", "Retriiving Neaby Restaurants", true, true) arnt 允许在 onCreate() 之外:
class MyAsync extends AsyncTask<Void, Integer, Boolean>{ // this task is for populating the listview
@Override
protected void onPreExecute() {
//ProgressDialog.show(context, "Working", "Retreiving Neaby Restaurants");
pd = ProgressDialog.show(this, "Working", "Retreiving Neaby Restaurants", true, true); // initializes the progress dialog
pd.setCanceledOnTouchOutside(false); // on newer versions of android touching outside of the screen will close the dialog. I decided to make it only cancelable when the back button is pressed.
}
这有点令人不安,因为我想在异步任务开始时显示对话框,但在调用该任务之前不久。
所以我的问题是如何在 onPreExecute() 的异步任务中完成这项工作,我不想在用户真正开始使用异步任务之前显示()。