在 运行 计时表上延迟时间

Put delay in time while running the chronometer

我有一个 android 应用程序,其中包含倒数计时器 我为此使用了计时器.....

现在有这样一种情况,我使用 handler.postdelyed() 时,每 10 秒后,应该更改 1 秒,但在这种情况下,计时表会在 10 秒后启动 运行,但计时表的计时器会启动从 10 秒,而不是 1 秒。有什么方法可以让我的计时器延迟计时吗?

这是我的代码:

chronometer_life_gained.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
    @Override
    public void onChronometerTick(final Chronometer chronometer) {
        long delay = (206 / Long.parseLong(joint_per_day))*1000;
        System.out.println("delayer " + delay);
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
        if ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 60000) {
            long dy = TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase());
            final long hr = TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase())
                    - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase()));
            final long min = TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase())
                    - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase()));
            final long sec = TimeUnit.MILLISECONDS.toSeconds(SystemClock.elapsedRealtime() - chronometer.getBase())
                    - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase()));

            year_num_life_gained.setVisibility(View.GONE);
            year_text_life_gained.setVisibility(View.GONE);
            month_num_life_gained.setVisibility(View.GONE);
            month_text_life_gained.setVisibility(View.GONE);
            day_num_life_gained.setVisibility(View.GONE);
            day_text_life_gained.setVisibility(View.GONE);
            hour_num_life_gained.setVisibility(View.GONE);
            hour_text_life_gained.setVisibility(View.GONE);
            min_num_life_gained.setVisibility(View.GONE);
            min_text_life_gained.setVisibility(View.GONE);
            sec_num_life_gained.setVisibility(View.VISIBLE);
            sec_text_life_gained.setVisibility(View.VISIBLE);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

            params.addRule(RelativeLayout.CENTER_VERTICAL);
            params.addRule(RelativeLayout.ALIGN_PARENT_START);
            sec_num_life_gained.setLayoutParams(params);

            sec_num_life_gained.setText(Long.toString(sec));


        }
//
            }
        }, delay);

//    }

    }
});

提前致谢

你可以用CountDownTimer实现它,虽然它的名字是倒数计时器,但我们可以让它工作起来。

// Counter variable to show seconds increment
int counter = 4000;

// If you want to count until 60 seconds, put VALUE_IN_MILLIS_UNTIL_YOU_WANT_TO_COUNT_UP = 60000 (in milliseconds)
elapsedTimer = new CountDownTimer(VALUE_IN_MILLIS_UNTIL_YOU_WANT_TO_COUNT_UP, 5000) {
    @Override
    public void onTick(long l) {
// Logically, we need 
long elapsedTime = SystemClock.elapsedRealtime() - startTime - counter;
        long dy = TimeUnit.MILLISECONDS.toDays(elapsedTime);
        final long yr = dy / 365;
        dy %= 365;

        final long mn = dy / 30;
        dy %= 30;

        final long hr = TimeUnit.MILLISECONDS.toHours(elapsedTime)
                - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(elapsedTime));
        final long min = TimeUnit.MILLISECONDS.toMinutes(elapsedTime)
                - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(elapsedTime));
        final long sec = TimeUnit.MILLISECONDS.toSeconds(elapsedTime)
                - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(elapsedTime));
        // add 4000 as 4 seconds to counter value
        counter = counter + 4000;
        txView.setText(counter + " second");
    }
}.start();
private Runnable updateTimerMethod = new Runnable() {

    public void run() {
        long elapsedTime = SystemClock.elapsedRealtime() - startTime;
        long dy = TimeUnit.MILLISECONDS.toDays(elapsedTime);
        final long yr = dy / 365;
        dy %= 365;

        final long mn = dy / 30;
        dy %= 30;

        final long hr = TimeUnit.MILLISECONDS.toHours(elapsedTime)
                - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(elapsedTime));
        final long min = TimeUnit.MILLISECONDS.toMinutes(elapsedTime)
                - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(elapsedTime));
        final long sec = TimeUnit.MILLISECONDS.toSeconds(elapsedTime)
                - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(elapsedTime));

        myHandler.postDelayed(this, 2000);
    }

};