不要在 pause/resume 结束后重置计时器(很难解释)

Do not reset a timer after a pause/resume when it has already ended (difficult to explain)

我是 java android 的新手。我对 SO 做了很多研究,但找不到符合我的案例。

我用不同的计时器制作了一个 android 应用游戏。我实现了 pause/resume 方法,它工作正常但第二个计时器有大问题:我希望这个计时器在 return 的循环中已经完成时不要从暂停中恢复27=]。这个计时器对应于游戏中角色的动作。我希望我能很好地解释自己(对不起我的英语,我是法国人)。非常感谢您的帮助...

我的暂停:

 protected void onPause() {
        super.onPause();
        MyMediaPlayer.getMediaPlayerInstance().stopAudioFile();
        SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("lastActivity", getClass().getName());
        editor.commit();
        mCountDownTimer.cancel();
        mCountDownTimer = null;
        Timer2.cancel();
        Timer2 = null;
    }

我的简历:

 protected void onResume() {
        super.onResume();
        MyMediaPlayer.getMediaPlayerInstance().resumeAudio();
        if (mCountDownTimer == null) {
            mCountDownTimer = new CountDownTimer(mTimeRemaining, TIMER_INTERVAL) {
                @Override
                public void onTick(long millisUntilFinished) {
                    textView.setText(String.format(Locale.getDefault(), "%d sec.", millisUntilFinished / 1000L));
                    mTimeRemaining = millisUntilFinished;
                }

                @Override
                public void onFinish() {
                    Intent intent = new Intent(P39x.this, Lose15b.class);
                    Toast toast = Toast.makeText(P39x.this, "time has passed...\n" +
                            "\n" +
                            "YOU HAVE LOST !", Toast.LENGTH_LONG);
                    View toastView = toast.getView();
                    TextView toastMessage = (TextView) toastView.findViewById(android.R.id.message);
                    Typeface typeface = Typeface.createFromAsset(getAssets(), "SanvitoProLight.ttf");
                    toastMessage.setTypeface(typeface);
                    toastMessage.setTextSize(50);
                    toastMessage.setBackgroundResource(R.drawable.quiet);
                    toastMessage.setTextColor(Color.RED);
                    toastMessage.setGravity(Gravity.CENTER_VERTICAL);
                    toast.show();
                    getMediaPlayerInstance().stopAudioFile();
                    startActivity(intent);
                    finish();
                    MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.collapse);
                    mediaPlayer.start();
                }
            }.start();

            if (Timer2 == null) {
                Timer2 = new CountDownTimer(TIMERDURATION, TIMERINTERVAL) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        mTimeRemainin = millisUntilFinished;
                    }

                    @Override
                    public void onFinish() {
                        toast = Toast.makeText(P39x.this, "Find your way now", Toast.LENGTH_LONG);
                        View toastView = toast.getView();
                        TextView toastMessage = (TextView) toastView.findViewById(android.R.id.message);
                        Typeface typeface = Typeface.createFromAsset(getAssets(), "SanvitoProLight.ttf");
                        toastMessage.setTypeface(typeface);
                        toastMessage.setTextSize(40);
                        toastMessage.setBackgroundColor(Color.BLACK);
                        toastMessage.setTextColor(Color.MAGENTA);
                        toastMessage.setGravity(Gravity.CENTER_VERTICAL);
                        toast.show();
                    }
                }.start();

我希望 Timer2 在 activity.

的循环中已经完成时,不要从暂停中恢复 return

这是一个生命周期问题,将 timer2 移至 onCreate() 方法而不是 onResume,因为 onResume 在 onPause() 激活后被调用。

如果我对您的问题的理解正确,您希望第二个计时器在 activity 已经完成时不重新启动。我认为有不同的方法可以解决这个问题。一个解决方案是实现一个布尔变量,该变量保存计时器是否应该继续的信息。

Boolean startTimer = true;

在 Timer2 onFinish() 函数中,您将此 var 设置为 false

Timer2 = new CountDownTimer(...){
   ....
     @Override
     public void onFinish() {
      ....
      startTimer = false;
     }
                
                    

并在 onResume() 函数中使用它:

protected void onResume(){
   ...
   if(startTimer){
       Timer2.start()
   }
}