title: Android: Stop a handler at a certain time in an Intent Service
body: <p>I have an intentService that starts a handler, but after a certain amount of time I need to stop the handler. I'm not sure how to do this. I have the class below, but I'm just not sure how to stop the handler once the time is reached, or when a certain amount of hours/min passes. I would like this to be as efficient as possible please.</p>
  
  <pre><code>public class RedirectService extends IntentService {
  
     private Handler handler;
     private Runnable runnable = new Runnable() {
         @Override
         public void run() {
             foobar();
             handler.postDelayed(this, 2000);
         }
     };
  
     public LockedRedirectService() {
         super("RedirectService");
     }
  
     @Override
     protected void onHandleIntent(Intent redirectIntent) {
         // Gets data from the incoming Intent
         int hour = redirectIntent.getIntExtra("hour", 0);
         int min = redirectIntent.getIntExtra("minute", 0);
  
  
  
         handler.postDelayed(runnable, 2000);
         handler.removeCallbacks(runnable);
     }
  }
  </code></pre>