Last active
January 2, 2016 23:58
-
-
Save shallowlong/8379283 to your computer and use it in GitHub Desktop.
The advanced handler to set the runnable task and set time postpond
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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