Skip to content

Instantly share code, notes, and snippets.

@bastami82
Created January 3, 2018 11:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bastami82/ea0d984d5940df37490ff24fedf474c1 to your computer and use it in GitHub Desktop.
Save bastami82/ea0d984d5940df37490ff24fedf474c1 to your computer and use it in GitHub Desktop.
using Android handler as a timer to check a condition
private Runnable runnable;
private Handler handler;
private boolean condition;
runnable = () -> {
if (condition) {
handler.removeCallbacks(runnable); //stop handler
} else {
handler.postDelayed(runnable, 500); //check a condition every half a second (until condition is met)
}
};
handler.post(runnable);
/// some where in your code this conditin needs to get updated i.e condition = true; (other wise this check will go forever ;)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment