Runnable:
private final int interval = 1000; // 1 Second private Handler handler = new Handler(); private Runnable runnable = new Runnable(){ public void run() { Toast.makeText(MyActivity.this, "C'Mom no hands!", Toast.LENGTH_SHORT).show(); } }; ... handler.postAtTime(runnable, System.currentTimeMillis()+interval); handler.postDelayed(runnable, interval);
Message:
private final int EVENT1 = 1; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case Event1: Toast.makeText(MyActivity.this, "Event 1", Toast.LENGTH_SHORT).show(); break; default: Toast.makeText(MyActivity.this, "Unhandled", Toast.LENGTH_SHORT).show(); break; } } }; ... Message msg = handler.obtainMessage(EVENT1); handler.sendMessageAtTime(msg, System.currentTimeMillis()+interval); handler.sendMessageDelayed(msg, interval);
References
https://stackoverflow.com/questions/1877417/how-to-set-a-timer-in-android