有没有办法以15秒的间隔运行Asynk任务直到两分钟?

时间:2016-07-22 06:03:44

标签: android android-asynctask android-timer

我需要在每隔五十秒的时间间隔内运行asynk任务直到两分钟。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:2)

试试这个

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)
    }

`     叫它--TimerTask(0);