title: Android - Run a thread repeatingly within a timer
body: <p>First of all, I could not even chose the method to use, i'm reading for hours now and someone says use 'Handlers', someone says use 'Timer'. Here's what I try to achieve:</p>
  
  <p>At preferences, theres a setting(checkbox) which to enable / disable the repeating job. As that checkbox is checked, the timer should start to work and the thread should be executed every x seconds. As checkbox is unchecked, timer should stop.</p>
  
  <p>Here's my code:</p>
  
  <p>Checking whether if checkbox is checked or not, if checked 'refreshAllServers' void will be executed which does the job with timer.</p>
  
  <pre><code>boolean CheckboxPreference = prefs.getBoolean("checkboxPref", true);
                  if(CheckboxPreference == true) {
                      Main main = new Main();
                      main.refreshAllServers("start");
                  } else {
                      Main main = new Main();
                      main.refreshAllServers("stop");
                  }
  </code></pre>
  
  <p>The refreshAllServers void that does the timer job:</p>
  
  <pre><code>public void refreshAllServers(String start) {
  
      if(start == "start") {
  
          // Start the timer which will repeatingly execute the thread
  
      } else {
  
          // stop the timer
  
              }
  </code></pre>
  
  <p>And here's how I execute my thread: (Works well without timer)</p>
  
  <pre><code>Thread myThread = new MyThread(-5);
                  myThread.start();
  </code></pre>
  
  <p>What I tried?</p>
  
  <p>I tried any example I could see from Google (handlers, timer) none of them worked, I managed to start the timer once but stoping it did not work.
  The simpliest &amp; understandable code I saw in my research was this:</p>
  
  <pre><code>new java.util.Timer().schedule( 
          new java.util.TimerTask() {
              @Override
              public void run() {
                  // your code here
              }
          }, 
          5000 
  );
  </code></pre>