If you want to change the time all the time with different values, I suggest you use
schedule(TimerTask task, long time)
Every time you have a new time from the database, just create a new one Timer()
, for example,
time = getNewTimeFromDB();
createNewTask(time);
....
private void createNewTask(long time) {
Timer timer=new Timer();
TimerTask timerTask=new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
}
};
timer.schedule(timerTask,time);
}
It's good that you do not need to cancel the timer every time, because it is designed to run once.
source
share