每 30 秒调用任何方法的最佳方法是什么

问题描述 投票:0回答:1

我需要每 30 秒调用一个方法,最好的方法是 Handler、Observable.interval() 还是有其他有效的方法

android rx-java
1个回答
0
投票

对于科特林:

Timer().scheduleAtFixedRate(timerTask {
      // your code here
        }, 30000, 30000
)

对于Java:

Runnable helloRunnable = () -> {
            // your code here
};

ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(helloRunnable, 0, 30, TimeUnit.SECONDS);
© www.soinside.com 2019 - 2024. All rights reserved.