Search code examples
androidmultithreadingrunnableprogressdialog

Android ProgressDialog not dismissing from Thread


i'm developing an android App. The user registration process calls a service that sends an email so it takes several seconds, like 5 or 6 seconds,that's why I execute that task within a thread. The problem is, the Dialog is never dismissing. It stays rolling and the user can do nothing. Here's my code:

try
        {
        final ProgressDialog progDailog = new ProgressDialog(ActividadAltaUsuario.this);

        new Thread(new Runnable() 
        {
        @Override
        public void run()
        {
            try
            {
                URL url = new URL("slowWS");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");

                InputStream in = new BufferedInputStream(conn.getInputStream());
                String response = IOUtils.toString(in, "UTF-8");
                final JSONObject jsonPrincipal = new JSONObject(response);
                Boolean success = jsonPrincipal.get("status").toString() == "true";

                if (success)
                {
                    ActividadAltaUsuario.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            progDailog.show(ActividadAltaUsuario.this, "Sendind email");
                        }
                    });

                    final String idUsuario = jsonPrincipal.get("idUsuario").toString();
                    URL url2 = new URL("anotherSlowWS");
                    HttpURLConnection conn2 = (HttpURLConnection) url2.openConnection();
                    conn2.setRequestMethod("POST");
                    InputStream in2 = new BufferedInputStream(conn2.getInputStream());
                    String response2 = IOUtils.toString(in2, "UTF-8");
                    JSONObject jsonRtaMail = new JSONObject(response2);
                    //finish();
                }
                else
                {
                    //finish();
                    showToast(jsonPrincipal.get("message").toString());
                }

                ActividadAltaUsuario.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progDailog.dismiss();
                    }
                });
            }
            catch (Exception e)
            {
                e.printStackTrace();
            } 
        }
        }).start();

        }
        catch(Exception e)
        {
            Log.e("log_tag", "Error in http connection" + e.toString());
        }

Can anybody help me? Thanks!


Solution

  • AsyncTask would be a better approach instead of thread, Replace your network call from thread to use AsyncTask. You can use something like this

    private class LongOperation extends AsyncTask<Void, Void, Void> {
    
        @Override
        protected String doInBackground(Void... params) {
            //Main stuff that needs to be done in background
    
        }
    
        @Override
        protected void onPostExecute(Void result) {
            //Post Execution this method will be called, handle result accordingly
            //You can dismiss your dialog here
        }
    
        @Override
        protected void onPreExecute() {
            //Do initialization relative stuff here
            // Initialize your dialog here.
        }
    }
    

    As both onPostExecute() and onPreExecute() work on main thread you can show and dismiss your dialog in this methods.