ProgressDialog does not work in an asynchronous task. No errors, just not displayed:

 class ProgressDownloader extends AsyncTask<String, String, String> { ProgressDialog PD; Downloader dow = new Downloader(); @Override protected void onPreExecute() { PD.show(getApplicationContext(), null, "Loading Please Wait...", true); } @Override protected String doInBackground(String... arg0) { dow.downloadFile(arg0[0], arg0[1]); return null; } protected void onPostExecute(Boolean result) { PD.dismiss(); } } 
  • one
    where ProgressDialog PD = new ProgressDialog(); - Saidolim 4:21 pm
  • @Saidolim tried, it still does not work ... Downloading works fine, but ProgressDialog does not work! - LevBazdyrev
  • then put this progresbar in layout and then use it through id. will definitely work - Saidolim

1 answer 1

It works for me like this:

  @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(MainActivity.this); pDialog.setMessage("Загрузка Подождите..."); pDialog.setIndeterminate(false); pDialog.setCancelable(false); pDialog.show(); // открываем диалог } @Override protected String doInBackground(Void... params) { //Что то делаем, например запрос JSON } @Override protected void onPostExecute(String strJson) { pDialog.dismiss(); // Закрываем диалог } 
  • Excellent .. Thank you, I earned it too. Only one moment remains, after the download is finished, the dialogue does not close ... - LevBazdyrev
  • one
    Try to check the most likely does not work onPostExecute () - Lukmanov
  • Yes, he does not see it, only judging by the errors, this function is not present in the interface itself ... - LevBazdyrev
  • Found a mistake, Thank you all for your help. - LevBazdyrev
  • one
    It is in the interface. When you start an asynchronous stream, the onPreExecute () function is initially started, then the main doInBackground () function is started, which does the main work, and if it completes successfully, the onPostExecute () function is launched. One of the reasons why onPostExecute () does not work is the failure of the code in the doInBakcground () function. try poyskay in it. - Lukmanov