简体   繁体   中英

Java/Android Countdown timer

i'm trying to make an countdown timer on Android with java. There are a timer a 25 minute study timer and a 5 minute break timer. I have both timers working. However, I don't know how to cancel/clear the timers. If I click I start a study timer and break timer they will both run simultaneously and I don't want that. I want the original timer to cancel when another timer is put on. This is the code.

btnStart.setOnClickListener(new OnClickListener() {

        public void onClick(View v){

btnStart.setEnabled(false);
btnBreak.setEnabled(true);

breakBoolean = false;




            CountDownTimer timer;
            long amountOfStudyTime = 1500000; //30 seconds (may cause problems)
            long countDownInterval = 1000; //1 second

            //Initialise the countdown timer

            timer = new CountDownTimer(amountOfStudyTime, countDownInterval){

                public void onTick(long millisUntilFinished){

                    if( studyBoolean = false ) {

                        tViewTime.setText("CountDownTimer Canceled/stopped.");
                        cancel();
                        breakBoolean = true;

                    }else{
                        //display remaining seconds to user

                        tViewTime.setText(""+String.format("%d min, %d sec", 
                                TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished),
                                TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - 
                                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));

                    }
                }

                public void onFinish(){
                    //When countdown is finished do...

                    breakBoolean = true;

                     int currentScore = Integer.parseInt(editScore.getText().toString());

                     int finalScore = currentScore + 5;

                    editScore.setText(Integer.toString(finalScore));


                    tViewTime.setText("Done");  

                }

            }.start();
        }
    });



    //Set a click listener for break button
            btnBreak.setOnClickListener(new OnClickListener() {

                public void onClick(View v){



                    btnStart.setEnabled(true);
                    btnBreak.setEnabled(false);


                    studyBoolean = false;



                    CountDownTimer timer2;
                    long amountOfBreakTime = 300000; //30 seconds (may cause problems)
                    long countDownInterval = 1000; //1 second

                    //Initialise the countdown timer

                    timer2 = new CountDownTimer(amountOfBreakTime, countDownInterval){

                        public void onTick(long millisUntilFinished){

                            if( breakBoolean = false ) {


                                cancel();
                                studyBoolean = true;

                            }else{
                                //display remaining seconds to user

                                tViewTime.setText(""+String.format("%d min, %d sec", 
                                        TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished),
                                        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - 
                                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));

                            }
                        }

                        public void onFinish(){
                            //When countdown is finished do...


                            tViewTime.setText("Done");  
                            studyBoolean = true;
                        }

                    }.start();
                }
            });

Create two CountDownTimer objects in your activity and cancel them depending on your button selected :

    public class MainActivity extends Activity {

     boolean breakIsRunning = false;
     boolean startIsRunning = false;

        Button btnStart,btnBreak;

        CountDownTimer startTimer = new CountDownTimer(amountOfStudyTime, countDownInterval)
        {

            @Override
            public void onFinish() {
               //do something
               startIsRunning = false;
            }

            @Override
            public void onTick(long arg0) {
                //do something
                startIsRunning = true;
            }

        };



    CountDownTimer breakTimer = new CountDownTimer(amountOfBreakTime, countDownInterval)
            {

                @Override
                public void onFinish() {
                   //do something
                   breakIsRunning  = false;
                }

                @Override
                public void onTick(long arg0) {
                    //do something
                    breakIsRunning = true;
                }

            };


 //->OnCreate() - >Buttons code

     btnStart.setOnClickListener(new OnClickListener().. { // your listener code here
          if(breakIsRunning)
             breakTimer.cancel();
          startTimer.start();
    }

     btnBreak.setOnClickListener(new OnClickListener().. { //
           if(startIsRunning)
              startTimer.cancel();
           breakTimer.start();
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM