CountdownTimer并退出活动

时间:2012-01-16 14:45:49

标签: android android-activity countdowntimer

我制作了一个游戏模块,其中用户被授予一分钟回答问题,然后调用意图来显示结果,我的问题是当我在中间退出活动时,它仍然显示结果无论我做什么活动,在游戏中完成的时间。

我的计时器代码是

class MyCount extends CountDownTimer{
    public MyCount(long millisInFuture, long countDownInterval) {
    super(millisInFuture, countDownInterval);
    }
    @Override
    public void onFinish() {


        Intent finish= new Intent(QuestionScreen.this,ResultScreen.class);
        finish.putExtra("noofques", Integer.toString(totalquestions));
        finish.putExtra("correct", Integer.toString(score));
        startActivity(finish);
        overridePendingTransition( R.anim.slideinleft, R.anim.slideoutleft);
    }
    @Override
    public void onTick(long millisUntilFinished) {
        if(millisUntilFinished>60000)
        timerview.setText((millisUntilFinished/(1000*60))+1 +" minutes left");
        else
        {
        timerview.setText(millisUntilFinished/1000 +" seconds left");
        }
    }
    }

和退出游戏的代码是

    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
        alertbox.setMessage("Do you really want to quit");
        alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                Intent info = new Intent(QuestionScreen.this,com.preciselabs.mental_skills.MenuScreen.class);
                info.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                finish();
                startActivity(info);
                overridePendingTransition( R.anim.slideinright, R.anim.slideoutright);
                }
        });
        alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
           }
        });
        alertbox.show();
    return true;
    }
    return super.onKeyDown(keyCode, event);
    }

我的问题的任何解决方案都将非常感谢

1 个答案:

答案 0 :(得分:0)

退出活动时使用belove代码..

  mCount.cancel();
  mCount=null;

和onfinish()中的boolean为startActivity(完成);