在这里,我创建了启动计时器的方法,下面的方法更新计时器:
private void startTimer()
{
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
progress++;
pb.setProgress((int)progress*100/((int)millisUntilFinished/1000));
}
@Override
public void onFinish()
{
progress++;
pb.setProgress(100);
//Vibration
if (Build.VERSION.SDK_INT >= 26)
{
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(150, VibrationEffect.DEFAULT_AMPLITUDE));
Toast.makeText(MainActivity2.this,"Done",Toast.LENGTH_SHORT).show();
}
else
{
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createWaveform(new long[]{150}, new int[]{VibrationEffect.EFFECT_CLICK},-1));
}
}
}.start();
}
private void updateCountDownText()
{
//time in minutes and seconds
int minutes = (int)(mTimeLeftInMillis/1000)/60;
int seconds = (int)(mTimeLeftInMillis/1000)%60;
//formating the above to appear as String
String timeLeftFormatted = String.format("%02d:%02d",minutes,seconds);
timer.setText(timeLeftFormatted);
}
“pb”是我的progressbar的名称。它总是比倒计时提前2分钟完成,我不知道如何同步它们。此外,在完成时,由于某种原因不会触发振动,即使它以前触发过。”“进度”作为全局变量初始化为零。
2条答案
按热度按时间evrscar21#
但我看不出你什么时候初始化progressbar。如果将pb.max(或java中的setmax)=100,并在每次单击时调用一个方法:
其中millisstartvalue是您在新倒计时计时器(mtimeleftinmillis)中输入的第一个值
在onfinish中,您不必为progressbar添加新值。
1cklez4t2#
以下是解决方案: