1

timeLeft is working for playing music file, but how to work with onResume? So I can pause the activity, then resume with left timer.

Here is my code:

public void countdownTimer () {

    final TextView mTextField = (TextView)findViewById(R.id.timer);
    Count = new CountDownTimer(TIMER, 1000) {
        public void onTick(long millisUntilFinished) {

             mTextField.setText("" +(millisUntilFinished / 60000));
             long timeLeft = millisUntilFinished / 1000;
             if(timeLeft <= 4 && timeLeft >=2 && tgbutton.isChecked())
             {
                 mSoundPool.play(sixthMusicFile, 1f, 1f, 1, 0, 1f);
             }
             if(timeLeft <= 1 && tgbutton.isChecked())
             {
                mSoundPool.play(seventhMusicFile, 1f, 1f, 1, 0, 1f);
                vibrate(); 
             }

         }
         public void onFinish() {
             Count.setText("done!");
         }
      }.start();
}

EDITED: Finally, I got some answer. Working code here: Android CountDown Timer with Pause, Resume and Cancel button. Many thanks to everybody who help me before.

Bonnie7
  • 19
  • 1
  • 10

1 Answers1

1

try this activity :

    public class MyActivity extends AppCompatActivity {

                    long TIMER = 1000000;
                    long timeLeft = 0;
                    private CountDownTimer Count;
                    SharedPreferences sharedPreferences;

                    @Override
                    protected void onCreate(@Nullable Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        sharedPreferences = getSharedPreferences("App_shared_preferenced", Context.MODE_PRIVATE);

                    }

                    @Override
                    protected void onResume() {
                        super.onResume();
                        timeLeft=  sharedPreferences.getLong("leftTime",0);
                        if(timeLeft>0)
                        countdownTimer(timeLeft);
                        else countdownTimer(TIMER);
                    }

                    @Override
                    protected void onPause() {
                        super.onPause();
                        sharedPreferences.edit().putLong("leftTime",timeLeft).commit();
                        if(Count != null){                                                                
                          Count.cancel();
                        }
                    }

                    public void countdownTimer(long t) {
                          if(Count != null) Count.cancel();
                        final TextView mTextField = null;// Your text view
                        Count = new CountDownTimer(t, 1000) {
                            public void onTick(long millisUntilFinished) {

                                mTextField.setText("" + (millisUntilFinished / 60000));
                                timeLeft = millisUntilFinished / 1000;
                                if (timeLeft <= 4 && timeLeft >= 2 && tgbutton.isChecked()) {
                                    mSoundPool.play(sixthMusicFile, 1f, 1f, 1, 0, 1f);
                                }
                                if (timeLeft <= 1 && tgbutton.isChecked()) {
                                    mSoundPool.play(seventhMusicFile, 1f, 1f, 1, 0, 1f);
                                    vibrate();
                                }
                            }

                            public void onFinish() {
                                Count.setText("done!");
                                timeLeft = 0;
                            }
                        }.start();
                    }
                }
vivek mahajan
  • 521
  • 3
  • 16
  • I think your code is good. But, when I pause, log say timeLeft = 0. And when I back to the app, log say timeLeft = 0. I don't know what is this happen. – Bonnie7 May 13 '17 at 08:23
  • you want it do it if user removes app from background or killed if activity get's distorted ?, if Yes then I have updated my Answer code. – vivek mahajan May 13 '17 at 08:30
  • Just stop the countdown when user press "home button". And when app is play again, timer is start with timeLeft. Now, it's like 2 countdown run together with different time.. – Bonnie7 May 13 '17 at 08:35
  • What is @Nullable at onCreate? Still show double countdown when I back to my app from pressing "home button". BTW, I call countdown like this: `public void displayNextQuestion() { countdownTimer(TIMER); } ` – Bonnie7 May 13 '17 at 08:47
  • it's an annotation that it can be null be ready to compiler, it wont get affect, Add these lines before creating new CountDownTimer on countdownTimer() method : if(Count != null){ Count.cancel(); } – vivek mahajan May 13 '17 at 08:52
  • Pause are working, log show **pause: 45 sec**. But when I resume countdown start like a first time use. Its like countdownTimer(timeLeft); at onResume is not working. Thanks for help me.. – Bonnie7 May 13 '17 at 09:18