简体   繁体   English

在循环延迟后调用方法?

[英]Calling a method after a delay in a loop?

I found this answer: 我找到了这个答案:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    //Do something after 100ms
  }
}, 100);

but when I put that in a loop it activates instantaneously. 但是当我将其放入循环中时,它会立即激活。 If I use sleep() it will prevent the code inside from executing, so I can't figure out how I can do this. 如果我使用sleep(),它将阻止内部代码的执行,因此我不知道该怎么做。

There are several approaches how can you do this. 有几种方法可以做到这一点。

For instance, 例如,

    new Thread(new Runnable() {
      @Override
      public void run() {
try {
    Thread.sleep(100L);
} catch (InterruptedException e) {
}
    //your background code.
      }
    }).start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM