androidandroid-asynctaskandroid-timer

Is there any way to run Asynk task till two minutes in interval of 15 seconds?


I need to run asynk task till two minutes in interval of every fiften seconds . Is there any way to do this ?


Solution

  • try like this

    void TimerTask(int count)
        {
            if(count>8) // 8 cyclses,because 60*2/15
                return;
    
            Timer myTimer = new Timer(); // Create timer
            myTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    // run your async task there
    
                    TimerTask(count++); //OnExecute
    
                }
            }, 0L, 15L * 1000);//every 15 sec (0L - seconds waiting for start)
        }
    

    ` Call it - TimerTask(0);