-1

I have a requirement, where I need to create a timer task which will execute the function after every 10 sec. There is reset Button, on click of that reset Button I want to reset my time from 10 sec to 30 sec. Now after 30 sec when it execute the function I need to reset my timer again to 10 sec. I tried using Handler , TimerTask and CountDownTimer, but not able to achieve the requirement. Can anyone suggest me the best way of solving this problem

// OnCreate of Activity
if (timerInstance == null) {
            timerInstance = Timer()
            timerInstance?.schedule(createTimerTask(), 10000L, 10000L)
}

private fun createTimerTask(): TimerTask {
        return object : TimerTask() {
            override fun run() {
                Log.d("TimerTask", "Executed")
                //presenter?.onCountdownTimerFinished(adapter.activeCallList, adapter.previousPosition)
            }
        }
}

//On Reset Button Click
timerInstance?.cancel()
timerInstance = Timer()
timerInstance?.schedule(createTimerTask(), 30000L, 30000L)
Rahul
  • 1,667
  • 6
  • 21
  • 38

1 Answers1

1

When your button is pressed, you could cancel the submitted TimerTask and reschedule with a delay of 30sec and a period of 10sec ? https://docs.oracle.com/javase/8/docs/api/java/util/Timer.html#scheduleAtFixedRate-java.util.TimerTask-long-long-

  1. Cancel the first submitted task by calling .cancel on it.
  2. use 30000L, 10000L as delay and period on the schedule in the button

Example code :

package so20190423;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TimerTest {

    public static void main(String[] args) {
        System.out.println(new Date());
        Timer timer = new Timer();
        TimerTask task = newTask();
        timer.scheduleAtFixedRate(task, 10000L, 10000L);
        task.cancel();
        timer.scheduleAtFixedRate( newTask(), 30000L, 10000L);
    }

    protected static TimerTask newTask() {
        return new TimerTask() {

            @Override
            public void run() {
                System.out.println("YO");
                System.out.println(new Date());
            }
        };
    }

}

HTH!

Highbrainer
  • 750
  • 4
  • 15
  • I updated my question, but still timer is executing after 10 second only. My second question is if I manage to reschedule the time to 30 seconds, then after 30 second how can I reschedule it again to 10 second. Can you check my code – Rahul Apr 23 '19 at 16:47
  • Still One problem is there. We are reschedule the timer to 30 sec on some action like reset button click. But after completion of 30 sec, we need to again reschedule my timer to 10 sec and there is button for that one. – Rahul Apr 23 '19 at 17:31
  • What the given program does is : 1/ schedule the task every 10 sec 2/ cancel it and schedule it to run in 30 sec then every 10 sec. If you need to separate the 30 sec then the 10 sec fixed rate, use two different schedule calls (one while a delay of 30000 and no period, one with ne delay and a period of 10sec)! – Highbrainer Apr 24 '19 at 07:28