Skip to content Skip to sidebar Skip to footer

Progress Dialog Not Show On Screen

I edited my code according dear Mayank'answer but It does not show any message that is sended as input in displayMsg() method before method begines..I should say MethodTest() is

Solution 1:

Remember that AsyncTasks should ideally be used for short operations (a few seconds at the most.)

Try to learn more about AsyncTask and there are so many mistakes in your code

  1. Do not call doInBackground() manually.

    dc.doInBackground(totalMsg); // Error

  2. DisplayMsg() called several times, each time a new instance of class DisplayMsgClass created

    DisplayMsgClass dc = new DisplayMsgClass(); // Error

  3. onPreExecute()textView.setText("Hello !!!"); // NullPointerException. textView.setText() is called without initializing it.

Caution

Do not call AsyncTask.execute() more than one on a same intance. For eg:

DisplayMsgClassdisplayMsgClass=newDisplayMsgClass();  
displayMsgClass.execute();  
displayMsgClass.execute(); //Error, IllegalStateException  

will show you a basic demo based on you implementation and you can simply modify it according to your own way.

publicvoidMethodTest() {

    // execute tasknewDisplayMsgClass().execute("Download now");
}

/*
public void MethodTest() {
    DisplayMsg("method 1 is running");
    Method1();

    DisplayMsg("method 2 is running");
    Method2();

    DisplayMsg("method 3 is running");
    Method3();

}

private int DisplayMsg(String msg) {
    totalMsg += msg;
    DisplayMsgClass dc = new DisplayMsgClass();
    dc.doInBackground(totalMsg);
}
*/privateclassDisplayMsgClassextendsAsyncTask<String, Integer, String> {

    @OverrideprotectedvoidonPreExecute() {
        super.onPreExecute();

        // retrieve the widgets
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        textView = (TextView) findViewById(R.id.textv1);


        textView.setText("Download initialized");
        progressBar.setVisibility(View.VISIBLE);
    }

    @OverrideprotectedvoidonProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
    }

    @OverrideprotectedStringdoInBackground(String... Messages) {

        // read commandsString command = Messages[0];
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return"Download completed";
    }

    @OverrideprotectedvoidonPostExecute(String result) {

        //invoked on the UI thread after the background computation finishes
        progressBar.setVisibility(View.INVISIBLE);
        textView.setText(result);
    }
} 

Solution 2:

The reason why you are seeing onProgressUpdate(Progress...) called at the end of if(...) is because publishProgress(Progress... values) posts a message to an internal handler, this internal handler later processes the message to update the progress. In other words, publishProgress(Progress... values) doesn't synchronously call onProgressUpdate(Progress...) See implementation here: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/os/AsyncTask.java#L649

This is necessary because publishProgress(Progress... values) is expected to be called from doInBackground(Params...), which is on the worker thread for an AsyncTask, whereas onProgressUpdate(Progress...) happens on the UI thread so the UI can reflect the progress change. By posting a message to the handler on the UI thread, the progress info can be synced from worker thread to the UI thread without blocking either thread.

Solution 3:

Try below code

privateintDisplayMsg(String msg) {
    totalMsg += msg;
    DisplayMsgClassdc=newDisplayMsgClass();
    dc.runner.execute(totalMsg);
}

Hope it will work

:)GlbMP

Solution 4:

create progress bar in xml layout and set its visibility gone by default and create code as sample

// AsyncTask .

privateclassDownloadWebPageTaskextendsAsyncTask<String, Integer, String> {

    @OverrideprotectedvoidonPreExecute() {
        //textView.setText("Hello !!!");
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        progressBar.setVisibility(View.VISIBLE);
        super.onPreExecute();
    }

    @OverrideprotectedvoidonProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);

    }

    @OverrideprotectedStringdoInBackground(String... urls) {
        String response = "";
        for (String url : urls) {
            DefaultHttpClient client = newDefaultHttpClient();
            HttpGet httpGet = newHttpGet(url);
            try {
                HttpResponse execute = client.execute(httpGet);
                InputStream content = execute.getEntity().getContent();

                BufferedReader buffer = newBufferedReader(newInputStreamReader(
                        content));
                String s = "";
                while ((s = buffer.readLine()) != null) {
                    response += s;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return response;
    }

    @OverrideprotectedvoidonPostExecute(String result) {
        progressBar.setVisibility(View.INVISIBLE);
        textView.setText(result);
    }
}

Post a Comment for "Progress Dialog Not Show On Screen"