How to make an Android program 'wait'

Viewed 96572

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

8 Answers

You can use the Handler class and the postDelayed() method to do that:

Handler h =new Handler() ;
h.postDelayed(new Runnable() {
    public void run() {
                 //put your code here
              }

            }, 2000);
}

2000 ms is the delayed time before execute the code inside the function

Related