Track each minute in countdown timer

前端 未结 1 1041
小蘑菇
小蘑菇 2021-01-03 14:24

I want to show a toast message after completion of each minute.I am using the count down timer and show the timer in a text view.Please help me to sort out this problem.Foll

相关标签:
1条回答
  • 2021-01-03 14:53

    Ok, You need to change your code little bit,

    1. You need to change your Seconds variable to class level
    2. You need to change your interval as follows,

      from counter = new MyCount (3600000,10); to counter = new MyCount (3600000,1000); because second argument is millisecond. I have done all the changes in below code,

      public class MainActivity extends Activity
      {
      TextView timerView;
      Button end,SOS;
      String output;
      MyCount counter;
      int length;
      
      // Change by Lucifer
      long seconds;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) 
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      
          timerView=(TextView)findViewById(R.id.tv_timer_track_me);
      
            //        counter = new MyCount (3600000,10);
              counter = new MyCount (3600000,1000);
              counter.start();
      }
      
      public String formatTime(long millis) 
      {
          output = "";
          seconds = millis / 1000;
          long minutes = seconds / 60;
          long hours=minutes/ 60;
      
          seconds = seconds % 60;
          minutes = minutes % 60;
          hours=hours%60;
      
          String secondsD = String.valueOf(seconds);
          String minutesD = String.valueOf(minutes);
          String hoursD=String.valueOf(hours);
      
          if (seconds < 10)
              secondsD = "0" + seconds;
          if (minutes < 10)
              minutesD = "0" + minutes;
      
          if (hours < 10)
              hoursD = "0" + hours;
      
          output = hoursD+" : "+minutesD + " : " + secondsD;
      
          return output;
      }
      
      public class MyCount extends CountDownTimer 
      {
          Context mContext;
      
          public MyCount(long millisInFuture, long countDownInterval) 
          {
              super(millisInFuture, countDownInterval);
          }
      
      
          public void onTick (long millisUntilFinished) 
          {
              timerView.setText ( formatTime(millisUntilFinished));
      
              if ( seconds == 0 )
              {
                  Toast.makeText( getApplicationContext(), "Done", Toast.LENGTH_LONG ).show();
              }
          }
      
          public void onFinish() {}
      
      }
      
      @Override
      public void onDestroy()
      {
          super.onDestroy();
          counter.cancel();
      }
      }
      
    0 讨论(0)
提交回复
热议问题