title: Handler.postDelayed function doesn't create time delays ? - Android
body: <p>Below I have   a Runnable "updater" ...and an OnClick function that uses Handler.PostDelayed function to run the runnable after a delay...</p>
  
  <p>After a little editing, cutting of useless parts here are the functions:
  (passtog = Toggle Button)</p>
  
  <pre><code> final Runnable updater = new Runnable() {
  
              @Override
              public void run() {
  
  
                  if (passTog.isChecked()) {              
                      now = System.currentTimeMillis();
                      time = now - init;
  
                      if (time &gt; 5000) {
                          Toast.makeText(getApplicationContext(), "WAKE UP !",
                                  Toast.LENGTH_SHORT).show();
  
                      }
  
                      handler.postDelayed(this, 25);
                  }
              }
          };
  
          passTog.setOnClickListener(new View.OnClickListener() {
  
              public void onClick(View v) {
                  init = System.currentTimeMillis();
                  flag = true;
  
                  handler.postDelayed(updater,
                          (new Random().nextInt(4000) + 3000));
  
              }
          });
      }
  </code></pre>
  
  <p><em><strong>Explaination</em></strong>
  Basically, The user toggles the Toggle button. Now it's on: The runnable can run completely (Everything is in the if block).
  If the user doesn't press the button again, and switches it off The app sends a Toast "Wake Up!" ..It runs and checks every 25 millisecs to update the time...</p>
  
  <p>Pretty straightforward... Yet I'm having a problem.</p>
  
  <p>Before the program actually gets to the runnable, I absolutely NEED there to be a minimum time delay of 3 seconds + Some Random value ... So it varies between 3 sec - 7 sec. It SHOULD vary between 3-7 , but it doesn't.</p>
  
  <p><em><strong>When I run it: The problem</em></strong>
  I notice that the first time, it works great... I get atleast a 3 sec delay + a random value= Perfect</p>
  
  <p>The second time, that is after the switch goes on ->off-> on : Now It acts like it doesn't see the +3000 ...and just the  ~randInt(4000) function... So it may give 0 sec or it may give 4 sec delay...</p>
  
  <p>In all my experience, I've never really come across this.. I've rewritten the entire code, My other apps use this function in exactly the same sytax and seem to do pretty great.. Why is this creating a problem ? Could the Toast's time possibly be causing a problem..</p>
  
  <p>How to solve this ?
  (I'm open to other methods, preferably quick to implement. I want a minimum 3 sec delay which I'm not getting for some reason... I need the UI to be responsive though So no thread sleeping.)</p>