4

Actually i wanted to ask can i give value from database to a timer delay?

    Timer timer = new Timer();
    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                  // whatever
                    }
                }
            });
        }
    };
    timer.schedule(timerTask,2000,**myDelay**); //here at my delay

Here at myDelay, can i give different values through database? Or it must be fixed?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Husnain
  • 137
  • 1
  • 9
  • 1
    it is fixed. if a different value comes in, you can cancel the current timer, then reschedule a new one with different time. – user1506104 Oct 02 '17 at 05:11
  • @user1506104 Actually i will change the timer delay for 10 times. So in this case do i have to cancel it everytime? Or it will work with just one line. – Husnain Oct 02 '17 at 05:13
  • 1
    sure you can, cancel the current timer and re-initiate with new value again – Heshan Sandeepa Oct 02 '17 at 05:23
  • no need to cancel the current timer. just create a timer that does not repeat using schedule(task, date). see my answer below – user1506104 Oct 02 '17 at 05:25

2 Answers2

1

If you are to change the time all the time with different values, I suggest you use

schedule(TimerTask task, long time)

Everytime you have a new time from DB, just create a new Timer() like so

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() {
                    // whatever
                }
            });
        }
    };
    timer.schedule(timerTask,time);
}

The good thing about this is you don't have to cancel the timer every single time because it is meant to run once.

user1506104
  • 6,554
  • 4
  • 71
  • 89
  • Isn't it needs a time like 2pm? What i am looking for is a different "VALUE IN MILLIS" after the app is launched. Example. When it launches the app it gets 2000m and after the next iteration of loop it must get 1500 and then 500. The sequence goes on – Husnain Oct 02 '17 at 06:24
  • use this instead: schedule(TimerTask task, long time); i updated the answer – user1506104 Oct 02 '17 at 06:55
0

May you should change your approach to the problem, create a function to return the time from the database FunctionToGetDelayFromDB();

Timer timer=new Timer();
long time = FunctionToGetTimeFromDB();
TimerTask timerTask=new TimerTask() {
    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                    // whatever
                    timer.schedule(timerTask, System.currentTimeMillis() + FunctionToGetDelayFromDB());
                }
            }
        });
    }
};
timer.schedule(timerTask, System.currentTimeMillis() + FunctionToGetDelayFromDB());

This should work for what you want to achieve...

rex
  • 421
  • 4
  • 12