Skip to content

Instantly share code, notes, and snippets.

@shallowlong
Last active January 2, 2016 23:58
Show Gist options
  • Save shallowlong/8379283 to your computer and use it in GitHub Desktop.
Save shallowlong/8379283 to your computer and use it in GitHub Desktop.
The advanced handler to set the runnable task and set time postpond
// the advanced handler will allow to postpone for a given time.
// instead of using the java.lang.Thread class, the new class is java.lang.Runnable class
android.os.Handler.Handler
android.os.Message
java.lang.Runnable
Handler msgHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// handler logic
// like: msg.what == ??
}
};
Runnable taskRun = new Runnable() {
@Override
public void run() {
// business logic
// like:
int what = 0;
msgHandler.sendMessage(msgHandler.obtainMessage(what));
}
};
// enable the handler with the runnable task
msgHandler.removeCallbacks(taskRun);
msgHandler.postDelayed(taskRun, 3000); // 3000 indicated 3000 milliseconds, in other word, 3 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment