我的android应用中有一个活动,向用户显示一个单词。如果用户可以在不到60秒的时间内猜出答案,则可以按下按钮并进入另一项活动。但是如果他做不到并且时间还没结束,则必须显示第三项活动。我该怎么办?线程或计时器之类的东西?
我尝试了线程处理,但是应用程序崩溃了。
答案 0 :(得分:1)
您可以通过使用处理程序来实现。
科特林
// declare this variables as attributes in you class
val handler = Handler()
val runnable = Runnable {
// Call something when it finishes
}
handler.postDelayed(runnable, 60_000) // Do something after 60 seconds
// and if you want to cancel the timer, you can cancel it this way
handler.removeCallbacks(runnable)
Java
// declare this variables as attributes in you class
Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
// Call something when it finishes
}
};
handler.postDelayed(runnable, 60_000);
// and if you want to cancel the timer, you can cancel it this way
handler.removeCallbacks(runnable);