1

我在网上搜索过,但我只发现用户想要在 asynctask 中显示对话框的情况,例如:protected void onPreExecute()。

{
                            super.onPreExecute();
                            pDialog = new ProgressDialog(NumericoComercial.this);
                            pDialog.setMessage("Actualizando ...");
                            pDialog.setIndeterminate(false);
                            pDialog.setCancelable(true);
                            pDialog.show();
                        }

我想知道的是,是否可以像通常那样在 onPreExecute() 和 onPostExecute() 中不显示任何对话框的情况下完成异步任务。

我在使用 asynctask 和有关 Window Leaked Error 的对话框时遇到了一些问题。我尝试过的事情是不向 Pre 和 Post 操作添加任何对话框,如下例所示。

class UpdateCandidatos extends AsyncTask<String, String, String> {


                    @Override
                    protected void onPreExecute() {

                    }


protected void onPostExecute(String file_url) {

                    }
                }

使用此方法可能会出现任何错误,例如活动完成时?

谢谢

4

2 回答 2

1

我建议您在 onPause() 方法中取消异步任务。这样,如果您的活动关闭,异步任务将不会尝试发布任何数据,并且不会调用 onPostExecute。

Hm, you shouldn't be having any errors with onPostExecute(), you can use async task without showing any data, its not mandatory. You dont even have to @override that function, just put Void i the declaration ( ...extends AsyncTask<...,...,Void> )

于 2013-10-03T10:50:39.283 回答
1

onPreExecute() and onPostExecute() are not required methods extending Aynctask then all the things you write in those methods (Dialog, ProgressDialog etc) are unnecessary for the correct working of your class.

class UpdateCandidatos extends AsyncTask<String, String, String> {

    protected Long doInBackground(String... urls) { 
        //add here your background work
    }

}

this is a very simplified and fully forking AsyncTask that do "something" in background showing no dialog.

Note: you will have to handle the task according to the lifecycle of your app

于 2013-10-03T11:03:04.747 回答