-2

I am trying to show ProgressDialog in AsyncTask(). In my code i am using handler.postdelayed to run AsyncTask. Without handler.postdelayed it's showing the progressdialog.

final Handler handler = new Handler();

handler.postDelayed(new Runnable() {

    public void run() {
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected void onPreExecute() {
                progress = ProgressDialog.show(getActivity(), "Updating Profile", "Please Wait", true);
                progress.show();
                try {
                    File file = new File(Environment.getExternalStorageDirectory().getPath(), "/myProfileData.txt");
                    if (file.exists()) {
                        try {
                            profileDatabase.updateProfileFromDB();
                            profile = new StructConfigParameters();
                            isUseAutoConfigProfileChecked = true;
                            profileDatabase.updateProfileFromDB();
                            File myFile = new File(Environment.getExternalStorageDirectory().getPath() + "/myProfileData.txt");
                            FileInputStream fIn = new FileInputStream(myFile);
                            BufferedReader myReader = new BufferedReader(
                            new InputStreamReader(fIn));
                            String aDataRow = "";
                            String aBuffer = "";
                            while ((aDataRow = myReader.readLine()) != null) {
                                aBuffer += aDataRow + "\n";
                            }
                            myReader.close();
                        } catch (Exception e) {
                            System.out.println("Exception In updateProfileFromDB 133 " + e);
                        }
                    }
                } catch (Exception e) {
                    System.out.println("Exception In updateProfileFromDB 22 " + e);
                }
            }

            @Override
            protected Void doInBackground(Void... params) {
                return null;
            }

            @Override
            protected void onPostExecute(Void res) {
                progress.dismiss();
            }
        }.execute();
    }
});
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
Vijay Laxmi
  • 165
  • 1
  • 21

4 Answers4

1

While you are using AsyncTask to call web service you should follow this things.

As you are already using AsyncTask you don't need to call it in handler you can show progress in onPreExecute method of AsyncTask.

public class myAsyncTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Show your progress dialog here
    }

    @Override
    protected Void doInBackground(Void... params) {
        // this method should only have code to call web service 
        // or background work which should not be related to UI
        // UI operations should avoid in this method
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // you can do UI work in this method
        // Dismiss your progress dialog at the end of all operaions.
    }

}

While in your case it should be

new AsyncTask<Void, Void, Void>() {

    @Override
    protected void onPreExecute() {
        progress = ProgressDialog.show(getActivity(), "Updating Profile", "Please Wait", true);
        progress.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            File file = new File(Environment.getExternalStorageDirectory().getPath(), "/myProfileData.txt");
            if (file.exists()) {
                try {
                    profileDatabase.updateProfileFromDB();
                    profile = new StructConfigParameters();
                    isUseAutoConfigProfileChecked = true;
                    profileDatabase.updateProfileFromDB();
                    File myFile = new File(Environment.getExternalStorageDirectory().getPath() + "/myProfileData.txt");
                    FileInputStream fIn = new FileInputStream(myFile);
                    BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
                    String aDataRow = "";
                    String aBuffer = "";
                    while ((aDataRow = myReader.readLine()) != null) {
                        aBuffer += aDataRow + "\n";
                    }
                    myReader.close();
                } catch (Exception e) {
                    System.out.println("Exception In updateProfileFromDB 133 " + e);
                }
            }
        } catch (Exception e) {
            System.out.println("Exception In updateProfileFromDB 22 " + e);
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void res) {
        progress.dismiss();
    }
}.execute();
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
0
 Try the followoing 

            ProgressDialog progressDialog = new ProgressDialog(mainActivity);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setCancelable(false);
            progressDialog.setMessage("Loading Please Wait");
            progressDialog.show();
hareesh J
  • 520
  • 1
  • 6
  • 21
0
    class LoadData extends AsyncTask<Void, Void, Void> 
    {
      private ProgressDialog prgDialog;

      @Override
     protected void onPreExecute() 
      {
        prgDialog = new ProgressDialog(DataActivity.this);
        prgDialog.setMessage("Please wait...");
        prgDialog.setCancelable(false);
        prgDialog.show();
        super.onPreExecute();
      }

      @Override
      protected Void doInBackground(Void... params) 
       {
         // do some work like loading data
        return null;
       }

      @Override
      protected void onPostExecute(Void result)
      {
        super.onPostExecute(result);
        prgDialog.cancel();
       // do some work when loading finish
      }

}
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
0

change

 progress = ProgressDialog.show(getActivity(), "Updating Profile",
                                "Please Wait", true);
                        progress.show();

to this

 progress = ProgressDialog.show(YourActivityName.this, "Updating Profile",
                                "Please Wait", true);
                        progress.show();
uday
  • 1,348
  • 12
  • 27