10

这是一个倒数计时器,我想在分钟和秒内打印。ex(300000mili)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

4 回答 4

68

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);
            }
于 2013-07-12T17:38:27.433 回答
5

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

blue.setText((millisUntilFinished / 60000)+":"+(millisUntilFinished % 60000 / 1000));
于 2013-07-12T17:34:08.353 回答
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();
于 2021-01-19T10:15:26.090 回答
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()
于 2021-07-08T14:58:21.573 回答