I need to write a helper method which I can use in various places in the app to essentially make it 'sleep' for N milliseconds.It looks like Handler.postAtTime may be one way to do it, but I'd like any code snippets if available.
Asked
Active
Viewed 772 times
0
-
1Is it the main thread of the application that needs to be put to sleep for N milliseconds? what do you mean by making app to sleep? – Suresh Nov 29 '10 at 14:22
-
2You should not put app to sleep, especially not the main UI thread. This will affect UI drawing and will possibly make your app look sluggish. – Peter Knego Nov 29 '10 at 14:26
-
I am with Suresh - we need more info to help. Delaying operation X for Y ms would be easy, but it would let other ui operations and background processes continue normally. Alternatly you may be looking to u[pdate or check something every N ms, which is also doable. We just need to know more. As I said to Spencer below, you should not put your manin thread to sleep as it will essentiually freeze up the Android UI, and irritate users, and potentially cause FC warnings. – Eddie Nov 29 '10 at 14:26
3 Answers
5
You did not say why you need your app to "sleep".
Assuming you need to run a task after some time:
Handler h = new Handler();
h.postDelayed(new Runnable() {
public void run() {
// do something here
}
}, 1000); // 1000 ms delay

Peter Knego
- 79,991
- 11
- 123
- 154
-
The reason I need to do this is I want to display a message on the screen, have it displayed for 60 seconds, then display another message. There is nothing specific going on during the 60 seconds that I'm waiting to complete, so AsyncTask doesn't seem to apply. – charlest Nov 29 '10 at 14:40
-
-
Don't create a new Handler every time you post a message, but other than that this is the approach you want. – adamp Nov 29 '10 at 16:29
-
I tried putting this code in a Helper method I call, with nothing in the // do something here part, but get a force close: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare(). This sounds like the better way to go, but how do I make it a general utility method I can call to accomplish this? – charlest Dec 02 '10 at 01:34
2
If you don't mind blocking the thread, an alternative to Thread.sleep() is SystemClock.sleep().
Benefit is that it's a one-liner, as it ignores the InterruptedException so you don't need to handle it.
More info on http://developer.android.com/reference/android/os/SystemClock.html.
As already stated, you should avoid calling this on the main UI thread as it will cause your app to become unresponsive and potentially show the dreaded dialog we all hate to see (please wait or force close.)

ddewaele
- 22,363
- 10
- 69
- 82
1
Are you looking for something like this?
try {
//Put the thread to sleep for the desired amount of time (milliseconds)
Thread.currentThread().sleep(1000);
}
catch(InterruptedException ie){
}
This will put the thread you are calling it from to sleep for the amount of time you specify.

SpencerElliott
- 885
- 5
- 16
-
2I would warn against this method. The Android framework strongly discourages locking the UI thread at any time, which is essentially what your code is doing. – Eddie Nov 29 '10 at 14:24
-
+1 Eddie. Yes, one should never `sleep()` the main UI thread. This is extremely bad practice. – Peter Knego Nov 29 '10 at 14:27
-
On the plus side, this does work. On the minus side, it sounds like it' there's a better way, please let me know. – charlest Nov 29 '10 at 14:37
-
Where did it mention that Thread.sleep pauses the UI thread ? What if it is called from an other thread ? – Aurelien Ribon Nov 29 '10 at 14:39
-
Yes, my understanding is that this wouldn't be run on the main UI thread. ANR's are a bad, bad thing. – SpencerElliott Nov 29 '10 at 16:37