The timer does not stop on Android

PROBLEM I have problems stopping the timer during development in Android.

The timer is already null when it comes to stopping it.

Then I move the timer initialization outside the method, just like TimerTask, which solves the zero problem, but still does not cancel when timer.cancel(); is called on it timer.cancel(); .

The following is an example where the timer is already null when it comes to stopping recording.

Timertask

My TimerTask initialized inside the class, but outside the method and codes below ...

 private TimerTask task = new TimerTask() { @Override public void run() { Log.e("TRACK_RECORDING_SERVICE","Timer Running"); } }; 

Timer and timer start

Then I have a startRecroding method that is called when I want to start the timer ...

 public void startRecording(){ timer = new Timer("Message Timer"); timer.scheduleAtFixedRate(this.task, 0, 1000); } 

Stop timer

Then I call the method below when I want to stop the timer ...

 public void stopRecording() { if (timer != null) { timer.cancel(); timer = null; } else { Log.e("TRACK_RECORDING_SERVICE","Timer already null."); } } 

Any help would be greatly appreciated.

+13
source share
7 answers
 timer = new Timer("Message Timer"); 

Here your timer object is not static , therefore timer.cancel(); overrides another instance of the Timer class. I suggest you create a static instance variable of the Timer class at the top of the class, as shown below,

 private static Timer timer; 
+19
source

in the run () method, check for a timer, then

 private TimerTask task = new TimerTask() { @Override public void run() { if (timer == null) cancel(); ... } 

cancel the operation.

+2
source
 if(waitTimer != null) { waitTimer.cancel(); waitTimer.purge() waitTimer = null; } 
+1
source

Try this example ....

  TimerTask mTimerTask; final Handler handler = new Handler(); Timer t = new Timer(); int nCounter = 0; //function for start timer public void doTimerTask() { mTimerTask = new TimerTask() { public void run() { handler.post(new Runnable() { public void run() { nCounter++: //your code ..... ...... } }); }}; // public void schedule (TimerTask task, long delay, long period) t.schedule(mTimerTask,0,50); // } //function for stop timer public void stopTimerTask(){ if(mTimerTask!=null){ Log.d("TIMER", "timer canceled"); mTimerTask.cancel(); nCounter = 0; } } 

// use the above two functions for the start and stop timer.

0
source

I know this late, but I also ran into this problem in my project and hope that my solution can give people some ideas. What I did in my project as shown below:

 Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { //TODO Update UI } }; public void stopTimer() { if (timer != null) { handler.removeCallbacks(runnable); timer.cancel(); timer.purge(); timer = null; } } public startTimer() { timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { handler.post(runnable); } }, 0, 100); } 

I think the missing in previous answers removeCallbacks .

0
source

Just in case, if someone else comes here to find a solution to this problem, here is my experience.

I start a timer in the service.

 startForegroundService(mServiceIntent); timer = new Timer(); 

When you update a service, you do not have to cancel it first, you just call startForegroundService (mServiceIntent); Once again. If you do not cancel the timer before updating the service, the original timer will still run in the background and call methods, even if you stop the timer in the updated new service.

Thus, to summarize, stop your timer before updating or updating a background task. Hope this helps someone.

0
source

So the problem was that instantiation did not actually stop the timer.

Every time I called:

 timer = Timer() timer!!.scheduleAtFixedRate(object : TimerTask() { override fun run() { //something } }, delay, period) 

He created another instance, so the old instance was still working somewhere without the ability to stop it.

Therefore, I just created it when the timer is reset to zero so that no previous instance works and does not work in the background.

 if(timer == null) { timer = Timer() timer!!.scheduleAtFixedRate(object : TimerTask() { override fun run() { // something } }, delay, period) } 

Then just cancel it and set it to zero.

 fun stopTimer() { if (timer != null) { timer!!.cancel() timer!!.purge() timer = null } } 
0
source

Source: https://habr.com/ru/post/920573/


All Articles