Wednesday, December 1, 2010

[android-developers] Re: What's the best way to have your app 'sleep' for N milliseconds?

You are right:

private static final int WAKE_UP_DELAY = 5000;
private static final int WAKE_UP_CALL = 0;
private Handler handler;

handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.what==WAKE_UP_CALL) {
// change message, your code goes here...

handler.sendMessageDelayed(handler.obtainMessage(WAKE_UP_CALL),
WAKE_UP_DELAY);
return true;
}
return false;
}
}
);


handler.sendMessageDelayed(handler.obtainMessage(WAKE_UP_CALL),
WAKE_UP_DELAY);

// do something...

Emanuel Moecklin
1gravity LLC

On Dec 1, 4:47 am, Doug <beafd...@gmail.com> wrote:
> On Nov 30, 7:50 am, Emanuel Moecklin <1gravity...@gmail.com> wrote:
>
> > I would recommend using something like:
>
> This was actually unnecessarily complex.  There's no need to post a
> whole new runnable to a handler that, in turn, sends a message to the
> same handler.  You can just send the delayed message to the handler
> and you're done.
>
> Doug

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment