Eric February 2016

(android studio) mTimer and button

mTimer.schedule(new TimerTask()
    {
        @Override
        public void run()
        {
            randomNumber = rand.nextInt(8) + 1;
            arrayList.add(randomNumber);

            runOnUiThread(new Runnable()
            {
                @Override
                public void run()
                {
                    textArrayList.setText("" + arrayList);
                    textRandomNumber.setText("" + randomNumber);

                    if (gameOptionNumber < arrayList.size())
                    {
                        layoutGameButton.setVisibility(View.VISIBLE);

                        if(enterClicked == false)
                        {
                            mTimer.cancel();
                        }
                    }
                }
            });
        }
    }, 3000, 3000);

...

else if(v == buttonEnter)
    {
        TextView output = (TextView)findViewById(R.id.output);
        enterClicked = true;
        if(output.getText().length() != 0)
        {
            temp = Integer.parseInt(output.getText().toString());
            compareNumber(temp);
            output.setText("");
        }
    }

What I am trying to do here is the number changes every 3 seconds and I have to press numbers then enter button. If I don't press enterButton before 3 seconds until the next number comes up, the timer should stop.

I created enterClicked and assigned false to it then what I did is everytime the enterButton is clicked it changes enterClicked to true and the timer should keep going.

But unfortunately, timer just stops even though I pressed enterButton.

Should I place enterClicked at a different place?

Please help!!

Answers


Narayan Acharya February 2016

The problem here is that when you cancel the Timer you do not restart or make a new timer! http://developer.android.com/intl/pt-br/reference/java/util/Timer.html#cancel()

public void cancel ()

Cancels the Timer and all scheduled tasks. If there is a currently running task it is not affected. No more tasks may be scheduled on this Timer. Subsequent calls do nothing.

Since you have to restart the timer when enter is pressed, you kind of need to restart the timer before the next scheduled timer period. So when you cancel the timer make sure you make a new one and start that. (This might lead to memory hogging if the previous Timers are not garbage collected.)

Also I would recommend you use Handlers instead of TimerTasks. Read this - http://www.mopri.de/2010/timertask-bad-do-it-the-android-way-use-a-handler/

All the best :)


Doug Stevenson February 2016

If you want to schedule some work to happen on the main thread, consider using a Handler to accomplish this. With a small amount of effort you can schedule repeated units of work via messages or Runnables that make changes to views.

Don't use TimerTask for this. It's not really appropriate for most Android apps.

Post Status

Asked in February 2016
Viewed 1,168 times
Voted 5
Answered 2 times

Search




Leave an answer