title: Show progress dialog until task finished its work
body: <p>Ok i have a program which needs to wait until android enables wifi adapter completely. I have this activity code and it works, but honestly i don't think that this is proper way of waiting to some task to be finished ( in this case , android needs to enable wifi ).</p>
  
  <pre><code>public class MainActivity extends Activity implements Runnable {
  
  ProgressDialog pd;
  WifiManager wm;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      wm = (WifiManager) getSystemService(WIFI_SERVICE);
  
      if(!wm.isWifiEnabled()) {
      pd = ProgressDialog.show(this, "Stand by", "Doing work");
  
      Thread t = new Thread(this);
      t.start();
      }
  
  
  }
  
  
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.activity_main, menu);
  
      return true;
  }
  
  
  
  
  
  
  @Override
  public void run() {
  
      wm.setWifiEnabled(true);
      while(wm.getWifiState() != 3) {
  
          try {
              Thread.sleep(1000);
          } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
      }
  
  
      pd.dismiss();
      }
  }
  </code></pre>
  
  <p>Can someone please tell me, how is the proper way of waiting a program from executing until some task is finished? So program scenario:</p>
  
  <ol>
  <li>If wifi is disabled, execute if statement ( show progress dialog and enable wifi )</li>
  <li>Show progress dialog until task is done ( in this case wifi enables completely)</li>
  <li>When wifi is enabled stop showing progress dialog</li>
  </ol>
  
  <p>Thanks in advance!</p>