Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop the timer after certain time?

I have an android application which has a timer to run a task:

time2.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            sendSamples();
        }
    }, sampling_interval, sending_interval);

Lets say sampling_interval is 2000 and sending_interval is 4000.

So in this application I send some reading values from a sensor to the server. But I want to stop the sending after 10000 (10 seconds).

What should I do?

like image 302
secret Avatar asked Apr 09 '13 06:04

secret


Video Answer


1 Answers

try

        time2.scheduleAtFixedRate(new TimerTask() {
            long t0 = System.currentTimeMillis();
            @Override
            public void run() {
              if (System.currentTimeMillis() - t0 > 10 * 1000) {
                  cancel();
              } else {
                  sendSamples();
              }
            }
...
like image 125
Evgeniy Dorofeev Avatar answered Oct 20 '22 00:10

Evgeniy Dorofeev