title: Can't stop Thread in Android with sleep-Timer and TimerTask
body: <p>I want to get some data from the network in a while loop until the user clicks a button on the screen, then stop to receive the data and show another activity. What I do to receive is as follows:</p>
  
  <pre><code>Thread listUpdater;
  </code></pre>
  
  <p>...</p>
  
  <pre><code>listUpdater = new Thread() {
      @Override
      public void run() {
          TimerTask task = new UpdateListTask();
  
          while (true) {
              task.run();
          }
      }
  };
  listUpdater.start();
  </code></pre>
  
  <p>...</p>
  
  <pre><code>public class UpdateListTask extends TimerTask {
  
       public UpdateListTask() {
       }
  
       @Override
       public void run() { 
          // get some data from network (asynchronically)
       }
  }
  </code></pre>
  
  <p>Once the user has clicked a button, I run the following code:</p>
  
  <pre><code>try {
      listUpdater.join();
  } catch (InterruptedException e) {
      e.printStackTrace();
  }
  </code></pre>
  
  <p>But it doesn't stop the run loop in the new thread/timer task, all code is still being run – what I don't want. What can I do to stop the code being run? Is there a way to stop the thread? Or can I do some inter-thread-boolean variable setting to stop the while loop? </p>
  
  <p>I tried everything but didn't succeed. Can anyone please help?</p>