title: How to loop a service?
body: <p>My service needs to check for something every minute and</p>
  
  <pre><code>while(true)
  {
     Thread.sleep(60000) 
     //REST OF CODE HERE//
  }
  </code></pre>
  
  <p>is not working. Making the application freeze and asking me to forcefully stop it.</p>
  
  <p>I am sure the problem is with the while loop but I thought it was the only way to infinitely repeat the service whenever the onStart() method executes.</p>
  
  <p>Any suggestions are appreciated.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I fixed it and in case you were wondering how the code looks like well there you go:</p>
  
  <pre><code>@Override
  public void onStart(Intent intent, int startId) {
      // TODO Auto-generated method stub
      super.onStart(intent, startId);
      Toast.makeText(this, "Service running", Toast.LENGTH_SHORT).show();
  
      handler = new Handler(){
  
          @Override
          public void handleMessage(Message msg) {
              // TODO Auto-generated method stub
              super.handleMessage(msg);
              Toast.makeText(protectionService.this, "5 secs has passed", Toast.LENGTH_SHORT).show();
          }
  
      };
  
  
  
      new Thread(new Runnable(){
          public void run() {
          // TODO Auto-generated method stub
          while(true)
          {
             try {
              Thread.sleep(5000);
              handler.sendEmptyMessage(0);
  
          } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } 
  
          }
  
                          }
      }).start();
  }
  </code></pre>
  
  <p>Basically the service is been called and the tasks of that service will be repeated every 5 secs in that case.</p>
  
  <p>I would like to thank vineetska, Janusz and inazaruk for suggesting to use Handlers. I would like to thank everyone who answered as well, your help was very much appreciated.              </p>