0

I am designing a bonus runnable such that for each question, if the user can complete the question earlier, there is a bonus score for time remaining for each question.

Code:

private Runnable Run_bonus_countdownHandler = new Runnable() 
{
    @Override
    public void run() 
    {
        if (game_pause ==false)
        {                   
            bonus_time_remaining = Math.max((bonus_time_remaining - 10),0);
            bonus_countdownHandler.postDelayed(this, 10);
            tv_bonus.setText("" + bonus_time_remaining);

            if (bonus_time_remaining == 0)
            {
                tv_bonus.setText("0");
                bonus_countdownHandler.removeCallbacks(this);
            }               
        }
        else
        {

        }
    }
};

Question:

If the user expires the bonus time period, ie the countdown reach 0, when the user starts another question, the bonus countdown can be run smoothly and at the end be removed. However, if the user can finish earlier for the question, for the next question the countdown would skip some numbers and quickly down to 0.

I would like to ask how could code be modified such that the runnable be properly removed before starting a new question?

Thanks a lot!

pearmak
  • 4,979
  • 15
  • 64
  • 122

1 Answers1

0

I enjoyed your question, and tried to answer and build on it... So I wrote some code to manage these timers in a separate thread, with a simple callback mechanism for notifications.

There is a QuestionTimer class with a simple interface that handles the timed questions in a thread-safe manner, and an activity (MainActivity) to demonstrate how to use it. The QuestionTimer provides its listener with the remaining time through a callback, so you can easily add this as a bonus or add more time to the next question.

You can find the code here: https://gist.github.com/anonymous/182c13a4961515781be6

Hope this helps!

Stéphane
  • 6,920
  • 2
  • 43
  • 53
  • wow thanks for your detailed reply! I will study that for the time being as this area is quite new for me..really thanks a lot! – pearmak Jul 04 '15 at 16:33