Search code examples
javaandroidandroid-studiowear-os

My progressbar does not sync with my count down timer


Here I have made this method for starting my timer and the one below it updates the timer:

 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" is the name of my progressbar. It keeps finishing earlier than the countdown by 2 minutes and I don't know how to synchronize them. Also upon completion the vibration is not triggered for some reason even though it did before. "progress" is initialized as zero as a global variable.


Solution

  • Here is the solution:

    long millisInFuture;
    
    private void startTimer() {
        millisInFuture = mTimeLeftInMillis;
        mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mTimeLeftInMillis = millisUntilFinished;
                updateCountDownText();
                long millisPassed = millisInFuture - mTimeLeftInMillis;
                progress = (int) (millisPassed * 100 / millisInFuture);
                pb.setProgress(progress);
            }
    
            @Override
            public void onFinish() {
                pb.setProgress(100);
                Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    VibrationEffect effect =
                            VibrationEffect.createOneShot(150, VibrationEffect.DEFAULT_AMPLITUDE);
                    vibrator.vibrate(effect);
                    Toast.makeText(MainActivity2.this, "Done", Toast.LENGTH_SHORT).show();
                } else {
                    vibrator.vibrate(150);
                }
            }
        }.start();
    }