如何从倒数计时器获取经过的时间发送到服务器

时间:2018-06-04 05:21:11

标签: android countdowntimer

我正在制作一个Quiz应用程序,我正在展示一个Countdowntimer。它在文本字段中显示倒数计时器,并显示进度条中的进度。但是在测验结束时,我希望用户将时间发送到服务器。我怎样才能获得经过的时间?

  countDownTimer = new CountDownTimer(10800000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            i[0]++;
            progressBar.setProgress((int) i[0] *100/(10800000/1000));

             timer.setText(String.format("%d : %d : %d", TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
                     TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
                     TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
        }

        @Override
        public void onFinish() {

            i[0]++;
            progressBar.setProgress(100);

        }
    };

2 个答案:

答案 0 :(得分:1)

我从@quick学习者那里得到了想法。

long elapsed_time;

 countDownTimer = new CountDownTimer(10800000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            i[0]++;
            progressBar.setProgress((int) i[0] *100/(10800000/1000));

             timer.setText(String.format("%d : %d : %d", TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
                     TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
                     TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));

             elapsed_time = 10800000-millisUntilFinished;
        }

        @Override
        public void onFinish() {
            i[0]++;
            progressBar.setProgress(100);

        }
    };

答案 1 :(得分:0)

试试这个解决方案

全局声明此变量并使用

private int elapsed_time = 10800000;

countDownTimer = new CountDownTimer(elapsed_time, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            i[0]++;
            progressBar.setProgress((int) i[0] *100/(10800000/1000));

             timer.setText(String.format("%d : %d : %d", TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
                     TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
                     TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
        }

        @Override
        public void onFinish() {

            i[0]++;
            progressBar.setProgress(100);
            sendRequestToServerAPI(elapsed_time);
        }
    };