How to repeat a task after a fixed amount of time in android?

后端 未结 5 1798
耶瑟儿~
耶瑟儿~ 2020-12-08 02:41

I want to repeatedly call a method after every 5-seconds and whenever I wish to to stop the repeated call of the method I may stop or restart the repeated call of the method

5条回答
  •  -上瘾入骨i
    2020-12-08 03:18

    You have to put this code inside the activity you want to call every 5 seconds

    final Runnable tarea = new Runnable() {   public void run() {
    hola_mundo();//the operation that you want to perform }}; 
    ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor();
    timer.scheduleAtFixedRate(tarea, 5, 5, TimeUnit.SECONDS);
    

提交回复
热议问题