如果没有打开某个活动,我怎么能告诉MyCountDownTimer退出?

时间:2015-03-16 21:39:45

标签: android android-intent

我正在进行的项目是每个问题都带有计时器的测验。持续30秒。我注意到如果你在计时器用完之前完成测试,计时器就不会停止运行。因此,如果您继续进行另一项测试,那么您尚未完成测试的通知将弹出并覆盖当前活动。

这是MyCountDownTimer类

public MyCountDownTimer(TextView textCounter, long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            this.textCounter = textCounter;
        }

    @Override
    public void onTick(long millisUntilFinished) {


      textCounter.setText(String.valueOf(millisUntilFinished / 1000));

    }

    @Override
    public void onFinish() {


        Intent retryIntent = new Intent(textCounter.getContext(), Retry.class);

           if (textCounter.getContext() instanceof Test1){
               whichTest = 1;
               retryIntent.putExtra("whichTest",whichTest);
           }
        if (textCounter.getContext() instanceof Test2){
            whichTest = 2;
            retryIntent.putExtra("whichTest",whichTest);
        }

        if (textCounter.getContext() instanceof Test3){
            whichTest = 3;
            retryIntent.putExtra("whichTest",whichTest);
        }
        if (textCounter.getContext() instanceof Test4){
            whichTest = 4;
            retryIntent.putExtra("whichTest",whichTest);
        }

        if (textCounter.getContext() instanceof Test5){
            whichTest = 5;
            retryIntent.putExtra("whichTest",whichTest);
        }
        if (textCounter.getContext() instanceof Test6){
            whichTest = 6;
            retryIntent.putExtra("whichTest",whichTest);
        }
        if (textCounter.getContext() instanceof Test7){
            whichTest = 7;
            retryIntent.putExtra("whichTest",whichTest);
        }

        textCounter.getContext().startActivity(retryIntent);



}

编辑:附加代码                 if(textCounter == null){

                myCountDownTimer.onFinish();

            }


        } else  {


           // String score7S = Integer.toString(score7);
            myCountDownTimer.cancel();
            Intent intent7 = new Intent(Test7.this, UsersAnswers7.class);
            intent7.putExtra("usersAnswers7", usersAnswers7);
            intent7.putExtra("isATOF7", isATOF7);
            intent7.putExtra("score7", score7);
            startActivity(intent7);
        }

1 个答案:

答案 0 :(得分:1)

CountDownTimer有一个cancel()方法。

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

每当您的用户完成测试时,请致电"及时和onFinish()不会被召唤。