Code example for Handler

Methods: sendMessageDelayed, obtain

0
     * Starts the generic timer. 
     */ 
    private void updateRunning() { 
        if (timerStarted != timerRunning) {
            if (timerStarted) {
                dispatchTimerUpdate(SystemClock.elapsedRealtime());
                timerHandler.sendMessageDelayed(Message.obtain(timerHandler, TICK_WHAT), 1000);
            } else { 
                timerHandler.removeMessages(TICK_WHAT);
            } 
            timerRunning = timerStarted;
        } 
    } 
 
    private Handler timerHandler = new Handler() {
        public void handleMessage(Message m) { 
            if (timerRunning) { 
                dispatchTimerUpdate(SystemClock.elapsedRealtime()); 
                sendMessageDelayed(Message.obtain(this, TICK_WHAT), 1000); 
            } 
        }