请使用TimerTask或Handler进行这些类型的工作。对您来说更容易。
对于 TimerTask:-
http://enos.itcollege.ee/~jpoial/docs/tutorial/essential/threads/timer.html
来自处理程序:-
http://examples.javacodegeeks.com/android/core/os/handler/android-handler-example/
如果您不喜欢尝试任何其他示例,以上只是示例。
处理程序句柄 = new Handler();
可运行可运行=新可运行(){
@Override
public void run() {
//what ever you want to do...
}
};
//如何调用任何方法,如 (onCreate)
处理 .postDelayed(runnable, 100);
*** *Timer 任务 public class JavaReminder { Timer timer;
public JavaReminder(int seconds) {
timer = new Timer(); //At this line a new Thread will be created
timer.schedule(new RemindTask(), seconds*1000); //delay in milliseconds
}
class RemindTask extends TimerTask {
@Override
public void run() {
System.out.println("ReminderTask is completed by Java timer");
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("Java timer is about to start");
JavaReminder reminderBeep = new JavaReminder(5);
System.out.println("Remindertask is scheduled with Java timer.");
}
}
输出 Java timer is about to start Remindertask 是用 Java timer 调度的。ReminderTask 由 Java 计时器完成 //这将在 5 秒后打印