0

I am trying to show a ProgressDialog for five seconds on user interface to block the UI so that my locationClient can get connected and after five seconds.

I am dismissing the dialog box.But the dialog box is not displaying at all and mobile screen goes black for 3-4 seconds.Is it happening because I am running this code On UiThread?. If yes is their any other approach i can follow to stop for 5 seconds and then execute the pending code.

My code for the thread is following:

public void startConnecting(final boolean isSearchWarnings) {
        mProgressDialog = ProgressDialog.show(this, "Please wait","Long operation starts...", true);
        MainActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= 5; i++) {
                    try {
                        if (mLocationClient.isConnected()) {
                            break;

                        } else {
                            Thread.sleep(1000);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
                mProgressDialog.dismiss();
                if (isSearchWarnings) {
                    new getWarnings().execute(false);
                } else {
                    new getWarnings().execute(true);
                }
            }
        });

    }
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
Ansh
  • 2,366
  • 3
  • 31
  • 51

1 Answers1

4

You have this

   MainActivity.this.runOnUiThread(new Runnable()

And then

   Thread.sleep(1000);

You are calling sleep on the ui thread. This blocks the ui thread which you should not do. You will get ANR.

http://developer.android.com/training/articles/perf-anr.html

If you want to cause a delay use handler

give a delay of few seconds without using threads

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256