cancelling a handler.postdelayed process
I do this to post a delayed runnable: myHandler.postDelayed(myRunnable, SPLASH_DISPLAY_LENGTH); And this to remove it: myHandler.removeCallbacks(myRunnable);
I do this to post a delayed runnable: myHandler.postDelayed(myRunnable, SPLASH_DISPLAY_LENGTH); And this to remove it: myHandler.removeCallbacks(myRunnable);
You can use: Handler handler = new Handler() handler.postDelayed(new Runnable()) Or you can use: handler.removeCallbacksAndMessages(null); Docs public final void removeCallbacksAndMessages (Object token) Added in API level 1 Remove any pending posts of callbacks and sent messages whose obj is token. If token is null, all callbacks and messages will be removed. Or you could also … Read more
final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { //Do something after 100ms Toast.makeText(c, “check”, Toast.LENGTH_SHORT).show(); handler.postDelayed(this, 2000); } }, 1500);
You’re almost using postDelayed(Runnable, long) correctly, but just not quite. Let’s take a look at your Runnable. final Runnable r = new Runnable() { public void run() { handler.postDelayed(this, 1000); gameOver(); } }; When we call r.run(); the first thing it’s going to do is tell your handler to run the very same Runnable after … Read more