Home » Android » java – Countdowntimer in minutes and seconds

java – Countdowntimer in minutes and seconds

Posted by: admin June 15, 2020 Leave a comment

Questions:

It’s a countdown timer and i want to print in minutes and seconds. ex(300000mili)5 minutes, and count 4:60, 4:59…
here is part of my code

final MyCounter timer = new MyCounter(300000,1000);
    blue.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            timer.start();
        }
    });
}
public class MyCounter extends CountDownTimer {

    public MyCounter(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onFinish() {
        blue.setText("Live");
    }

    @Override
    public void onTick(long millisUntilFinished) {
        blue.setText((millisUntilFinished/60000)+":"+(millisUntilFinished/5000));
        // i tried this
    }
}

And how can i put to vibrate when missing 20 seconds? and when it finished?
thanks and sorry for my bad english

How to&Answers:

http://developer.android.com/reference/android/os/CountDownTimer.html

TextView _tv = (TextView) findViewById( R.id.textView1 );
new CountDownTimer(30000, 1000) { // adjust the milli seconds here

    public void onTick(long millisUntilFinished) {
    _tv.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() {
       _tv.setText("done!");
    }
 }.start();

Since java 1.5 there is the java.util.concurrent.TimeUnit class

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

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.html

A TimeUnit represents time durations at a given unit of granularity and provides utility methods to convert across units, and to perform timing and delay operations in these units.

For Java versions below 1.5 or for systems that do not fully support the TimeUnit class the following equations can be used:

int seconds = (int) (milliseconds / 1000) % 60 ;
int minutes = (int) ((milliseconds / (1000*60)) % 60);
int hours   = (int) ((milliseconds / (1000*60*60)) % 24);

To vibrate

 Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            // Vibrate for 500 milliseconds
            v.vibrate(500);
  <uses-permission android:name="android.permission.VIBRATE"/> // permission in manifest

Edit:

Vibrate at 4 min 55 seconds for 500 milli seconds

 if((TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished)==4) &&
                    TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - 
                    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))==55)
            {

            Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            // Vibrate for 500 milliseconds
            v.vibrate(500);
            }

Answer:

Just use the modulo operator % to get the remaining milliseconds and divide by 1000 to convert to seconds.

blue.setText((millisUntilFinished / 60000)+":"+(millisUntilFinished % 60000 / 1000));