Java/Android/Firebase and being able to stop/restart a TimerTask in an onDataChange method

Tep :

I'm trying to start, end, and reset+restart a timer using two different if statements in the onDataChange method. The thing I can't get to work is getting the timer to restart if the first if statement is triggered again after the second one stops the timer. After the class declaration at the top, I have

    Timer firstTestTimer = new Timer();
    TimerTask increaseByTwo = new TimerTask() {
        @Override
        public void run() {
            incrementalCount = incrementalCount + 2;
            System.out.println(incrementalCount);
        }
    };

and in the onCreate method I have a Firebase reference with a ValueEventListener with the following code:

public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                String Motion1 = dataSnapshot.child("Enter").getValue().toString();
                if(Motion1.equals("YES")){
                    incrementalCount = 0;
                    firstTestTimer.schedule(increaseByTwo, 2000,2000);
                }
                if(Motion1.equals("STOP")){
                    firstTestTimer.cancel();
                    firstTestTimer.purge();
                    System.out.println("Total seconds elapsed since sensor showed YES: "+incrementalCount);
                    if(incrementalCount > 11){
                        System.out.println("The difference is greater than 11 seconds");
                    }
                }
            }

As of right now, when the Firebase value initially changes to "YES", the timer starts and every two seconds it increases incrementalCount by 2. When the Firebase value changes to "STOP", the timer shows the correct amount of seconds that have passed. Here's the problem: When the Firebase value changes to "YES" again, the app crashes and gives me some variation of java.lang.IllegalStateException: Task already scheduled or cancelled Is it possible to reset and restart the timer with a fresh count whenever the Firebase value changes to "YES"? Thank you!

Gaurav Mall :

As @ThomasKläger pointed out, you can't use a TimerTask again. The docs point out that:

A timer task is not reusable. Once a task has been scheduled for execution on a Timer or canceled, subsequent attempts to schedule it for execution will throw IllegalStateException.

It is like a plastic cup or spoon, built to be used only once. So every time you have to increment your timer, here's what you can do:

Leave the timer as it is and define the timer task object again:

if (Motion1.equals("YES")){
    incrementalCount = 0;

    //Define it again, I think it is a waste of time to check whether the task exists or not.
    increaseByTwo = new TimerTask() {
        @Override
        public void run() {
            incrementalCount = incrementalCount + 2;
            System.out.println(incrementalCount);
        }
    };

    firstTestTimer.schedule(increaseByTwo, 2000,2000);
}

The rest of the code is the same. This should work.

The documentation for TimerTask is here: TimerTask JDK 11

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=389382&siteId=1