Delay execution in Android
We want do something later sometimes, usually we implement it using Timer
, however, we can use Hanlder
to do it easily.
protected final void doSomething() {
// do some thing here
}
protected final void doSomething(long duration) {
Message msg = Message.obtain(mHandler, DO_SOMETHING);
mHandler.sendMessageDelayed(msg, duration);
}
protected Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == DO_SOMETHING) {
doSomething();
}
}
};
Written by Tony Zhou
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Android
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#