让我说我的Android应用程序中有一个方法
updateArrayList(double data);
如何在5秒内运行该方法并在此之后停止?
答案 0 :(得分:0)
你shuld尝试Thread.sleep(INT MILLIS);并在try catch块中执行操作
答案 1 :(得分:0)
使用计时器类这是完整的代码
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;
public class ReminderBeep {
Toolkit toolkit;
Timer timer;
public ReminderBeep(int seconds) {
toolkit = Toolkit.getDefaultToolkit();
timer = new Timer();
timer.schedule(new RemindTask(), seconds * 1000);
}
class RemindTask extends TimerTask {
public void run() {
System.out.println("Time's up!");
toolkit.beep();
//timer.cancel(); //Not necessary because we call System.exit
System.exit(0); //Stops the AWT thread (and everything else)
}
}
public static void main(String args[]) {
System.out.println("About to schedule task.");
new ReminderBeep(5);
System.out.println("Task scheduled.");
}
}
的代码
答案 2 :(得分:0)
你可以试试这个:
try { //you need the try and catch otherwise you get an error
Thread.sleep(5000); //this is the actual waiting
} catch (InterruptedException e) { //this can just be "Exception" it means that if the thread is told to start again, it wont cause an error.
System.out.println("This code was told to start again.");
//whatever you want to do if the code is told to start again during this.
}