0

I've used a CountDownTimer in an android project, and what happens, is, my OnFinish() fires while the CountDown is in its half way stage.

Here is the code :

public void ShowNotice(){
        cdt = new CountDownTimer(10000, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub
                Toast toast = Toast.makeText(con,"Game Starts In :"+String.valueOf(millisUntilFinished/1000),
                        Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
                toast.show();
            }

            @Override
            public void onFinish() {
                // TODO Auto-generated method stub
                RemoveButtonText();
            }
        };

        cdt.start();
    }

RemoveButtonText();// This method gets executed even when the count down isn't finished i.e Toast shows '5'.

Note : cdt is declared as a private member variable inside a class of type CountDownTimer.

Need help :)

4

1 回答 1

0

I've solved the problem.

public void ShowNotice(){
        cdt = new CountDownTimer(20000, 2000) {

            @Override
            public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub
                Toast toast = Toast.makeText(con,"Game Starts In :"+String.valueOf(millisUntilFinished/2000),
                        Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.BOTTOM, 0, 0);
                toast.show();
            }

            @Override
            public void onFinish() {
                // TODO Auto-generated method stub
                Toast toast = Toast.makeText(con,"Game Starts Now!",
                        Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.BOTTOM, 0, 0);
                toast.show();
                RemoveButtonText();
            }
        };

        cdt.start();
    }

The catch was in getting this

cdt = new CountDownTimer(20000, 2000)

right.

Thank you :)

于 2013-07-19T07:21:29.090 回答