如何在后退按钮上完成CountdownTimer

时间:2014-02-02 08:41:39

标签: android back countdowntimer

我想在Back按钮上完成我的CountdownTimer。我知道有一些类似的问题,但这对我的问题没有帮助。

我在onCreate()

中有以下代码
        @Override
        public void onFinish() {



            new CountDownTimer(10000, 550) {

                @Override
                public void onTick(long millisUntilFinished) {
                    for(int i = 0; i< arr.size(); i++){
                        Button aga = arr.get(i);
                        if(aga.getVisibility() == View.VISIBLE){
                            aga.setVisibility(View.GONE);
                        }
                    }
                    int zufall = (int) (Math.random()*23);
                    setNextButton(arr.get(zufall));
                }

                @Override
                public void onFinish() {

                     System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

                    new CountDownTimer(10000, 350) {

                        @Override
                        public void onTick(long millisUntilFinished) {
                            for(int i = 0; i< arr.size(); i++){
                                Button aga = arr.get(i);
                                if(aga.getVisibility() == View.VISIBLE){
                                    aga.setVisibility(View.GONE);
                                }
                            }
                            int zufall = (int) (Math.random()*23);
                            setNextButton(arr.get(zufall));

                            System.out.println("HHH");
                        }

                        @Override
                        public void onFinish() {
                           System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                            Intent highscore = new Intent (MainActivity.this, Highscore_eintragen.class);
                            highscore.putExtra("count", count);
                            startActivity(highscore);


                        }
                    }.start();
                }
            }.start();
        }
    }.start();

现在我需要的是,如果用户在CountdownTimer期间按下后退按钮来完成我的活动。针对这个问题的任何解决方案?

1 个答案:

答案 0 :(得分:2)

调用CountDownTimer.cancel():

http://developer.android.com/reference/android/os/CountDownTimer.html#cancel()

在创建期间存储对活动中当前CountDownTimer实例的引用,以便在Activity中访问它。