title: android, how to keep a thread running in service?
body: <p>I have created an app. It starts a service(onStartCommand type). 
  It creates a thread that keeps on monitor the clipboard:</p>
  
  <pre><code>private class MonitorTask extends Thread {
      @Override
      public void run() {
          mKeepRunning = true;
          while (true) {
              doTask(); //-this function uses the toast to show the string in clipboard
              try {
                  Thread.sleep(300);
              } catch (InterruptedException ignored) {
              }
              if (!mKeepRunning) {
                  break;
              }
          }
      }
  </code></pre>
  
  <p>I found that the after some time passed, the service was still there (according to running service manager), but the thread disappeared. How can I keep the thread running forever until user closes the app.</p>
  
  <p>I guess that may be InterruptedException, how can I use that catch to restart the Thread?</p>
  
  <p>Some old threads mentioned using AlarmManager to "startup the service and do something, close the service" at regular interval, but i dont think it is a good idea?</p>
  
  <p>Please let me know if there is a typical way to do so or any good idea? thanks in advance.</p>
  
  <p>Update
  in fact, i know there is ClipboardManager, but i know this one is not compatible to android 2.3.4. Besides, I would like to know if I want to create a thread in service, how can i reset it if it was killed? thanks</p>