在几分钟和几秒钟内倒数计时器

时间:2013-07-12 17:27:36

标签: java android eclipse

这是一个倒数计时器,我想在几分钟和几秒钟内打印。前(300000米)5分钟,计数4:60,4:59 ...... 这是我的代码的一部分

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
    }
}

如果失踪20秒我怎么能振动?什么时候结束? 谢谢,抱歉我的英文不好

4 个答案:

答案 0 :(得分:49)

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();

从java 1.5开始,有java.util.concurrent.TimeUnit类

 _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

TimeUnit表示给定粒度单位的持续时间,并提供实用方法来跨单位进行转换,并在这些单位中执行定时和延迟操作。

对于低于1.5的Java版本或对于不完全支持TimeUnit类的系统,可以使用以下等式:

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

振动

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

编辑:

振荡4分55秒,持续500毫秒

 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);
            }

答案 1 :(得分:5)

只需使用模运算符%获取剩余毫秒并除以1000即可转换为秒。

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

答案 2 :(得分:0)

 mCountDownTimer = new CountDownTimer(duration, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        
                        mTextViewAdTime.setText("Ad: " + String.format("%02d:%02d",
                                TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
                                TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
                                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
                        
                    }

                    @Override
                    public void onFinish() {
                        mTextViewAdTime.setText("Ad:");
                    }
                }.start();

答案 3 :(得分:0)

Ashwin 回答的 Kotlin 版本:

var mCountDownTimer = object : CountDownTimer(duration, 1000) {
        override fun onTick(millisUntilFinished: Long) {
            val text = "OTP is valid for ${String.format(
                "%02d:%02d",
                TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
                TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
                        TimeUnit.MINUTES.toSeconds(
                            TimeUnit.MILLISECONDS.toMinutes(
                                millisUntilFinished
                            )
                        ))} minutes. Please enter the OTP "
            binding.timer.text = text
        }

        override fun onFinish() {
            binding.timer.text = "Request OTP again to proceed."
        }
    }.start()