I am only seeing articles on how to do something after a certain amount of time, but I want to know how to do something FOR a certain amount of time in Android.
public void buttonClick(View v) {
currentUser.changeScore(10);
TextView tv = (TextView) findViewById(R.id.score);
tv.setText("Score: " + currentUser.getScore());
}
I want to the user to be able to press the button and gain points for 10 seconds after an Event has occured. After those 10 seconds, I want the user to lose 10 points if the button is pressed.
It’s simple. Every time your event happens, set a variable, like this:
It’s not like you’re really wanting anything to change on a regular basis during that interval – the event is just setting a new point in time as the boundary of when it’s “good” to click the button. No need for a timer or anything like that.
(For testability you may want to have a dependency-injected
Clocktype or something similar to provide the “current time” functionality, but the basic principle is the same.)