Do timer functions stop automatically when paused?

I have a timer that forces the function to run every minute, per minute. When the action is paused, the timer continues. I do not want it to work, as it is not necessary.

If it works on suspension, how can I prevent it?

A piece of chalk.

In onCreate () I have

//Respond to clock changing every minute, on the minute myTimer = new Timer(); GregorianCalendar calCreationDate = new GregorianCalendar(); calCreationDate.add(Calendar.MILLISECOND, (-1*calCreationDate.get(Calendar.MILLISECOND))); calCreationDate.add(Calendar.SECOND, -1*calCreationDate.get(Calendar.SECOND)); calCreationDate.add(Calendar.MINUTE, 1); //Update every one minute myTimer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { timerMethod(); } }, calCreationDate.getTime(), 60000); 

Inside the class (outside onCreate ()) I have:

 //Update the clock every minute protected void timerMethod() { this.runOnUiThread(Timer_Tick); } //end TimerMethod private Runnable Timer_Tick = new Runnable() { public void run() { int intTpHour = tpTimePicker.getCurrentHour(); int intTpMinute = tpTimePicker.getCurrentMinute(); displayMyTime(intTpHour, intTpMinute); } }; //end Runnable Timer Tick 
+4
source share
2 answers

In this case, you must implement your Timer object as a member of the instance of your activity, create and run it in the onResume() method of your activity and stop it in the onPause() method of this action; thus, it will only work if the action is in the foreground.

+2
source

In most cases, threads continue to run when activity is in the background. The system reserves the right to kill an activity and its associated process at any time. See the โ€œLife Cycle Managementโ€ section of the Dev Guide for more information .

+1
source

All Articles