I have a timer that causes a function to run every minute, on the minute. When the activity is paused will the timer continue to run. I dont want it to run as it is unecessary.
If it does run when paused, how can I prevent it?
Mel.
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);
Within the class (outside of 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
In that case you should implement your
Timerobject as an instance member of your activity, create and start it in theonResume()method of your activity and stop it in theonPause()method of that activity ; that way it will be running only when the activity is in the foreground.