Code example for Timer

Methods: scheduleAtFixedRate

0
        return super.onStartCommand(intent, flags, startId);
    } 
 
    public void startTimer(int position, int seconds) {
        handler.sendMessage(createStartMessage(position));
        Timer t = new Timer();
        t.scheduleAtFixedRate(new UpdateTask(position, seconds), 0, PERIOD);
        timers.put(position, t);
    } 
 
    public void stopTimer(int position) {
        timers.get(position).cancel();
        timers.remove(position);
        handler.sendMessage(createStopMessage(position));
    } 
 
    public void stopAllTimers() { 
        for (Map.Entry<Integer, Timer> entry : timers.entrySet()) {
            entry.getValue().cancel();
        } 
        timers.clear();