title: Display Toast from AsyncTask until event occurs
body: <p>I would like to display "Connecting ..." using Toast <strong>until</strong> the device is connected to a new network (more specifically when an IP address is obtained, see the while loop in doInBackground below). I am using AsyncTask for connecting to the network, but if I put Toast.makeText(...).show() inside onProgressUpdate() the toast calls will stack and end up displaying the text way longer than desired. My Connect class:</p>
  
  <pre><code>public class Connect extends AsyncTask&lt;Object,Void,Void&gt;{
      private static final String TAG="sure2015test";
      private Context context;
      private Network network;
      @Override
      protected Void doInBackground(Object... params) {
  
          this.network=(Network)params[0];
          this.context=(Context) params[1];
          final WifiConfiguration conf = new WifiConfiguration();
          conf.SSID = "\"" + network.ssid + "\"";
          if(network.password!=null){
              conf.preSharedKey = "\""+ network.password +"\"";
          }
          else{
              conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
          }
  
          if(network.manager.addNetwork(conf)==-1){
              Log.i(TAG, "Add network fail");
          }
          List&lt;WifiConfiguration&gt; configs = network.manager.getConfiguredNetworks();
          for (WifiConfiguration i : configs) {
              if (i.SSID != null &amp;&amp; i.SSID.equals("\"" + network.ssid + "\"")) {
                  network.manager.disconnect();
                  if(network.manager.enableNetwork(i.networkId, true)==false){
                      Log.i(TAG,"Enable Network fail ");
                  }
                  if(network.manager.reconnect()==false) {
                      Log.i(TAG, "Reconnect fail");
                  }
              }
          }
          WifiInfo info=network.manager.getConnectionInfo();
          while((info.getIpAddress())==0){
              //Wait until non-zero IP address (once a non-zero ip address is obtained, you are connected to the network)
              //Tried to use NetworkInfo.DetailedState to check if it was CONNECTED
              //However the api method said it remained in OBTAINING_IPADDR state even after it obtained an ip (must be bug)
              info=network.manager.getConnectionInfo();
              publishProgress();
              Log.i(TAG,"IP "+info.getIpAddress());
              try{
                  Thread.sleep(100);
              }
              catch(InterruptedException e){
                  Log.i(TAG,"Interrupted exception");
              }
          }
          return null;
      }
  
  
      @Override
      protected void onProgressUpdate(Void... values) {
          Toast.makeText(context, "Connecting ...", Toast.LENGTH_SHORT).show();
          super.onProgressUpdate(values);
      }
  
      @Override
      protected void onPostExecute(Void aVoid) {
          Toast.makeText(context,"Connected",Toast.LENGTH_SHORT).show();
          Intent newActivity = new Intent(context, Device.class);
          newActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          newActivity.putExtra("Device", network.ssid);
          context.startActivity(newActivity);
          super.onPostExecute(aVoid);
      }
  }
  </code></pre>