Reputation: 3
I have an activity in my android app that shows a word to the user. if the user could guess the answer in less than 60 seconds he would press a button and go to another activity. but if he could not do it and the time finishes, a third activity must show. How should I do it? with threading or timer or something like that?
I have tried threading but the app crashes.
Upvotes: 0
Views: 127
Reputation: 1061
You can achieve this by using Handler.
Kotlin
// 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);
Upvotes: 1