0

I use AsyncTask in my App for download a url. I use a ProgressDialog on onPreExecute() for waiting. But I cant see ProgressDialog while process finish and i see it for a moment. want to see it while downloading not after that. can any one help me. thanks my code is like this:

private class loadMoreListView extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        // Showing progress dialog before sending http request
        pDialog = new ProgressDialog(SingleMenuItemActivity.this);
        pDialog.setMessage("Please Wait ...");
        pDialog.isIndeterminate();
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected Void doInBackground(Void... unused) {
        runOnUiThread(new Runnable() {
            public void run() {

           // do something for downloading

        }
        });

        return (null);
    }


    protected void onPostExecute(Void unused) {
        // closing progress dialog
        pDialog.dismiss();
    }
}
  • 1
    The *whole point* of `doInBackground` is *NOT* to do it on the UI thread. `runOnUiThread` is only for updating the UI from the background process, when necessary. – David Conrad Sep 09 '14 at 19:44

3 Answers3

1
 runOnUiThread(new Runnable() {
            public void run() {
           // do something for downloading
        }

// do something for downloading, inside runOnUiThread, is wrong. runOnUiThread makes "do something for downloading" run on the UI Thread, and your application should crash for NetworkOnMainThreadException, you the app runs on a device with a version of android grater than GingerBread. Differently it will block the ui thread preventing him to draw your progress bar

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

The problem is in

runOnUiThread(new Runnable() {

        public void run() {

       // do something for downloading

    }
    });  

ProgressDialog will not update if the UI thread is still busy. There are many examples in SO for that. I don't understand why do you need UIthread. And as a rule of thumb - if you need Progress dialog, you need to let run asynctask in background thread,as it always do.Read the document

http://developer.android.com/reference/android/os/AsyncTask.html

You can use the below example

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

Soham
  • 4,397
  • 11
  • 43
  • 71
0

Firstly notice the "@override" header attached to all the AsyncTask Implemented methods e.g.

private class loadMoreListView extends AsyncTask<Void, Void, Void> {

    ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pDialog = new ProgressDialog(SingleMenuItemActivity.this);
        pDialog.setMessage("Please Wait ...");
        pDialog.isIndeterminate();
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        pDialog.cancel();

    }

}

Also Remove this from doInBackground unless you must do something on the UI.

runOnUiThread(new Runnable() {
        public void run() {

       // do something for downloading

    }
    });

You cannot do something for downloading on the runOnUiThread. doInBackground is meant for running background tasks like downloads etc. not visible to the UI.

user3144836
  • 4,060
  • 3
  • 27
  • 27