androidtimedelay

How to make an Android program 'wait'


I want to cause my program to pause for a certain number of milliseconds, how exactly would I do this?

I have found different ways such as Thread.sleep( time ), but I don't think that is what I need. I just want to have my code pause at a certain line for x milliseconds. Any ideas would be greatly appreciated.

This is the original code in C...

extern void delay(UInt32 wait){
    UInt32 ticks;
    UInt32  pause;

    ticks = TimGetTicks();
    //use = ticks + (wait/4);
    pause = ticks + (wait);
    while(ticks < pause)
        ticks = TimGetTicks();
}

wait is an amount of milliseconds


Solution

  • OK, first of all, never implement a delay with a busy loop as you're doing. I can see where that comes from -- I'm guessing that the palm pilot was a single-process device with no built-in sleep() function, but on a multi-process device, a busy loop like that just brings the entire processor to its knees. It kills your battery, prevents normal system tasks from running properly, slows down other programs or stops them completely, makes the device unresponsive, and can even cause it to get hot in your hands.

    The call you're looking for is Thread.sleep(). You'll need to set up to catch any interrupt exceptions that occur while you're sleeping.

    Second, with event-based user interfaces such as Android (or pretty much any modern GUI system), you never want to sleep in the UI thread. That will also freeze up the entire device and result in an ANR (Activity Not Responding) crash, as other posters have mentioned. Most importantly, those little freezes totally ruin the user experience.

    (Exception: if you're sleeping for short enough intervals that the user probably won't notice, you can get away with it. 1/4 second is probably ok, although it can make the application janky depending on the situation.)

    Unfortunately, there's no clean and elegant way to do what you want if what you're doing is porting a loop-based application to an event-based system.

    That said, the proper procedure is to create a handler in your UI thread and send delayed messages to it. The delayed messages will "wake up" your application and trigger it to perform whatever it was going to do after the delay.

    Something like this:

    View gameBoard;     // the view containing the main part of the game
    int gameState = 0;  // starting
    Handler myHandler;
    
    public void onCreate(Bundle oldState) {
      super.onCreate(oldState);
      ...
      gameBoard = findViewById(R.layout.gameboard);
      myHandler = new Handler();
      ...
    }
    
    public void onResume() {
      super.onResume();
      displayStartingScreen();
      myHandler.postDelayed(new Runnable() { 
        gotoState1(); 
      }, 250);
    }
    
    private void gotoState1() {
      // It's now 1/4 second since onResume() was called
      displayNextScreen();
      myHandler.postDelayed(new Runnable() { 
        gotoState2();
      }, 250);
    }
    ...