-1

I am using a timer that should every 12 seconds issues a warning. as shown in the code below i set the delay to 0 so that the timer starts immediately, but at run time, the below posted timer does not starts immediately it waits for the period set as a delay despit i set the delay to 0

in other words, the below timer should wait 0 sec as a delay and repeats itself every 12 seconds but what happens is, it at initial execution it waits 12 sec and repeat itself every 12 sec

any logical explaination why that is happening

code:

mVelWarningRule1Timer.scheduleAtFixedRate(
                     new SpeakOut(
                         getApplicationContext(), 
                         getApplicationContext()
                            .getResources()
                            .getString(R.string.rule_velocity_1)),
                         0,
                         getApplicationContext()
                            .getResources()
                            .getInteger(R.integer.int_assistWarning_interval)
                      );
appersiano
  • 2,670
  • 22
  • 42
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • Where do you set the period to 12 seconds? – hamena314 Jan 12 '16 at 13:15
  • @hamena314 it is in the 3rd parameter getApplicationContext().getResources().getInteger(R.integer.int_assistWarning_interval) – Amrmsmb Jan 12 '16 at 13:16
  • 1
    Change your example. Try to reduce it something that really shows the problem. The question you got is exactly a result of that. And: provide more information: update the question instead. So - rework your question to make it possible to understand what you are doing. – GhostCat Jan 12 '16 at 13:18
  • @user2121: The input format for the parameters of the method `scheduleAtFixedRate` is long. You are using `.integer.`, maybe something goes wrong there? – hamena314 Jan 12 '16 at 13:29

1 Answers1

0
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new RemindTask(), 0, 12000);

The first is the internal class. The second parameter is the delay. The third gives you the interval.

private class RemindTask extends TimerTask {
    @Override
    public void run() {
        try
        {
            getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    if (page > adapter.getCount()) {
                        page = 0;
                    } else {
                        viewPager.setCurrentItem(page++, true);
                    }
                }
            });
        }
        catch(Exception e)
        {
            timer.cancel();
        }
    }
}

This would make a carousel of pictures change every 12 seconds

Emdja
  • 64
  • 11
  • ok. the it is almost the same as the code posted above, the value of 12000 in my code above is just saved in values.xml file and i retriev it via getApplicationContext().getResources().getInteger(R.integer.int_assistWarning_in‌​terval) – Amrmsmb Jan 12 '16 at 13:21
  • maybe you implement this to early and your view can't show your specific behavior the first time... that's making it look like it waits 12s in the first interval – Emdja Jan 12 '16 at 15:54