title: AsyncTask example using loop
body: <p>I'm implementing an example of AsyncTask and starting from the example here <a href="https://stackoverflow.com/questions/9671546/asynctask-android-example">AsyncTask Android example</a>.</p>
  
  <p>However, I want to make a change.
  In my GUI I want to put a text box where the user can type something and, every 10 seconds, that name appears on the label.</p>
  
  <p>This way, if the user has entered a new value, it will be displayed on the label, instead of the static text "Executed", which is now displayed.</p>
  
  <p>My code is bellow.</p>
  
  <p>XML</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical" &gt;
          &lt;ProgressBar
              android:id="@+id/progressBar"
              style="?android:attr/progressBarStyleHorizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:indeterminate="false"
              android:max="10"
              android:padding="10dip"&gt;
          &lt;/ProgressBar&gt;
          &lt;Button
              android:id="@+id/button1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Start Progress" &gt;
          &lt;/Button&gt;
          &lt;TextView android:id="@+id/output"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="Replace"/&gt;
  
          &lt;EditText
              android:id="@+id/textInput"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:ems="10"
              android:inputType="textPersonName"
              android:text="Name" /&gt;
      &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The MainActivity.java</p>
  
  <pre><code>import android.app.Activity;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  
  public class MainActivity extends Activity implements View.OnClickListener {
  
      Button btn;
      EditText textInput;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          btn = (Button) findViewById(R.id.button1);
          textInput = findViewById(R.id.textInput);
          // because we implement OnClickListener we only have to pass "this"
          // (much easier)
          btn.setOnClickListener(this);
      }
  
      public void onClick(View view) {
          // detect the view that was "clicked"
          switch (view.getId()) {
              case R.id.button1:
                  new LongOperation().execute("");
                  break;
          }
      }
  
      private class LongOperation extends AsyncTask&lt;String, Void, String&gt; {
  
          String input;
  
          @Override
          protected String doInBackground(String... params) {
              for (int i = 0; i &lt; 5; i++) {
                  try {
                      Thread.sleep(1000);
                      input = textInput.getText().toString();
                  } catch (InterruptedException e) {
                      Thread.interrupted();
                  }
              }
              return "Executed";
          }
  
          @Override
          protected void onPostExecute(String result) {
              TextView txt = (TextView) findViewById(R.id.output);
              txt.setText(input); // txt.setText(result);
              // might want to change "executed" for the returned string passed
              // into onPostExecute() but that is upto you
          }
  
          @Override
          protected void onPreExecute() {}
  
          @Override
          protected void onProgressUpdate(Void... values) {}
      }
  }
  </code></pre>
  
  <p>Where will I put the code to listen? I want the label to change, checking every 10 seconds (for example) if there was a change in the text box ...</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Based on the answers, can I leave the infinite loop in this way? How do I stop this AsyncTask if I need to?</p>
  
  <pre><code>private class LongOperation extends AsyncTask&lt;String, String, String&gt; {
  
      String input;
  
      @Override
      protected String doInBackground(String... params) {
          while (true)
              try {
                  Thread.sleep(1000);
                  input = textInput.getText().toString();
                  publishProgress(input);
  
              } catch (InterruptedException e) {
                  Thread.interrupted();
                  return "Executed";
              }
      }
  
      @Override
      protected void onPostExecute(String result) {
  
      }
  
      @Override
      protected void onPreExecute() {}
  
      @Override
      protected void onProgressUpdate(String... values) {
          TextView txt = (TextView) findViewById(R.id.output);
          txt.setText(values[0]);
          // update the UI with Data received from publishprogress
      }
  }
  </code></pre>