I'm currently using a Timer and TimerTask to perform some work every 30 seconds.
My problem is that after each time I do this work I want to increment the interval time of the Timer.
So for example it starts off with 30 seconds between the timer firing but I want to add 10 seconds to the interval then so that the next time the Timer takes 40 seconds before it fires.
Here is my previous code:
public void StartScanning() {
scanTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
wifiManager.startScan();
scanCount++;
if(SCAN_INTERVAL_TIME <= SCAN_MAX_INTERVAL){
SCAN_INTERVAL_TIME = SCAN_INTERVAL_TIME + SCAN_INCREASE_INTERVAL;
t.schedule(scanTask, 0, SCAN_INTERVAL_TIME);
}
}
});
}};
Log.d("SCAN_INTERVAL_TIME ** ", "SCAN_INTERVAL_TIME ** = " + SCAN_INTERVAL_TIME);
t.schedule(scanTask, 0, SCAN_INTERVAL_TIME);
}
REFACTORED CODE
@Override
public void StartScanning() {
t.schedule(new ScanTask(),SCAN_INTERVAL_TIME);
}
class ScanTask extends TimerTask{
@Override
public void run() {
wifiManager.startScan();
scanCount++;
if(SCAN_INTERVAL_TIME < SCAN_MAX_INTERVAL)
SCAN_INTERVAL_TIME = SCAN_INTERVAL_TIME + SCAN_INCREASE_INTERVAL;
t.schedule(new ScanTask(), SCAN_INTERVAL_TIME);
}
}
It works now but is creating a new ScanTask() every time wasteful?